我有以下数组:
array = [{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}]
我想将它转换为json,如下所示:
json = {{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}}
所以我可以将mongodb中的所有内容存储在一个独特的文档中。
此致
答案 0 :(得分:0)
只需运行循环并等同......就像......
var obj = {};
for(var i=0; i<array.length; i++)
{
obj[i] = array[i]
}
它会做
{
0:{"id":144,"price":12500000},
1:{"id":145,"price":13500000},
2:{"id":146,"price":13450000},
3:{"id":147,"price":11500000},
4:{"id":148,"price":15560000}
}
因为您的JSON无效。
答案 1 :(得分:0)
不确定你在问什么
从数组或其他变量到json string =&gt;
var str = JSON.stringify(thing);
从json字符串到变量
var thing = JSON.parse(str);