我有以下 json对象:
var data = [{" name":" abc"," count":" [20.8,100]"}, {" name":" xyz"," count":" [40,100]"}]
注意数组用双引号:" [20.8,100]"
我想要双引号
预期输出:
var data = [{" name":" abc"," count":[20.8,100]}, {" name":" xyz"," count":[40,100]}]
任何帮助将不胜感激
答案 0 :(得分:3)
您可以使用JSON.parse
并将值分配给同一属性。
var data = [{"name":"abc", "count":"[20.8, 100]"}, {"name":"xyz", "count":"[40, 100]"}];
data.forEach(function (a) {
a.count = JSON.parse(a.count);
});
console.log(data);
答案 1 :(得分:0)
第1步:使用javaScript for...in循环迭代JSON。
第2步:使用JSON.parse()将JSON字符串( "[20.8, 100]"
)转换为JSON对象( [20.8, 100]
强>)。
工作演示:
var data = [
{"name":"abc", "count":"[20.8, 100]"},
{"name":"xyz", "count":"[40, 100]"}
];
for (var i in data) {
data[i].count = JSON.parse(data[i].count);
}
console.log(data);