这是我的代码
Swipe data = Array[2] :
0:object
id : "1" latitude : "76.23" longitude:"21.92"
1:object
id:"2" latitude:"10.23" longitude:"12.92"
var abc=[];
for(_i = 0; _i < swipe_data.length; _i++)
{
var tmp = swipe_data[_i];
var t1 = [swipe_data[_i]['latitude'], swipe_data[_i]['longitude']];
abc[tmp.id] = JSON.stringify(t1);
}
console.log(abc);
abc 的输出是 -
[1: "["76.2350","21.9253"]", 3: "["10.5650","21.3653"]", 6: "["60.5650","55.3653"]"]
这是json字符串
but i need output like this - {"1": [76.235,21.9253], "3": [10.565,21.3653], "6": [60.565,55.3653]}
对此有什么解决方案吗?
最佳答案
使用parseFloat
,
var abc = new Object();
for(_i = 0; _i < swipe_data.length; _i++){
var t1 = [parseFloat(swipe_data[_i]['latitude']), parseFloat(swipe_data[_i]['longitude'])];
abc[tmp.id] = t1
}
console.log(JSON.stringify(abc));
尝试运行这段代码,它应该可以工作。
注意:
abc
应该是Object
而不是Array
。- 在将值分配给
key
时不要JSON.stringify
。 - 在您的
对象
中更新了所有键值后,执行JSON.stringify
。
关于javascript - Json 结果 - 如何删除引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44193579/