gpt4 book ai didi

javascript - 我可以将字符串中的数组转换为数组吗? Javascript

转载 作者:行者123 更新时间:2023-11-30 09:23:55 25 4
gpt4 key购买 nike

我有如下数组

'[{"stuff_width":200,"_id":"cPEzHYNLHhyHbtK9MJ7F","stuff_weight":400,"stuff_length":100,"image":["cPEzHYNLHhyHbtK9MJ7F_0","cPEzHYNLHhyHbtK9MJ7F_1"],"quantity":5,"stuff_type":"Cunstruction Material \\/ Cement","stuff_height":300},{"stuff_width":200,"_id":"F5OhdfGnoHpffkpR82KK","stuff_weight":300,"stuff_length":400,"image":["F5OhdfGnoHpffkpR82KK_0"],"quantity":5,"stuff_type":"Furniture \\/ Wood Items","stuff_height":400}]'

我可以只删除第一个和最后一个单引号并将其用作简单数组吗?

我用过类似的东西

courier_items.slice(1, -1);

但它给了我如下结果

{"stuff_width":2,"_id":"ubcpQhAmIiGPyrqm7OpT","stuff_weight":12,"stuff_length":1,"image":["ubcpQhAmIiGPyrqm7OpT_0","ubcpQhAmIiGPyrqm7OpT_1"],"quantity":1,"stuff_type":"Cunstruction Material \/ Cement","stuff_height":12}

它仍然表现得像字符串。

我想要的不仅仅是对象

最佳答案

使用 JSON.parse 而不是 slice(),因为 JSON.parse 将解析 JSON 数组并以数组形式输出:

var str = '[{"stuff_width":200,"_id":"cPEzHYNLHhyHbtK9MJ7F","stuff_weight":400,"stuff_length":100,"image":["cPEzHYNLHhyHbtK9MJ7F_0","cPEzHYNLHhyHbtK9MJ7F_1"],"quantity":5,"stuff_type":"Cunstruction Material \\/ Cement","stuff_height":300},{"stuff_width":200,"_id":"F5OhdfGnoHpffkpR82KK","stuff_weight":300,"stuff_length":400,"image":["F5OhdfGnoHpffkpR82KK_0"],"quantity":5,"stuff_type":"Furniture \\/ Wood Items","stuff_height":400}]';

var res = JSON.parse(str);

console.log(res);

Parse the data with JSON.parse(), and the data becomes a JavaScript object.

关于javascript - 我可以将字符串中的数组转换为数组吗? Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49975112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com