我有一个JSON字符串,如下所示:
键值对是动态从服务器获取的,我事先不知道要期望什么数据。对于我使用的绘图库,需要JSON中的值是数字而不是字符串,例如
这是我的方法,但似乎无效:
[{
"id": "id2",
"index": "2",
"str": "str2",
"cent": "200",
"triplet": "222"
},
{
"id": "id3",
"index": "3",
"str": "str3",
"cent": "300",
"triplet": "333"
},
{
"id": "id4",
"index": "4",
"str": "str4",
"cent": "400",
"triplet": "444"
},
{
"id": "id5",
"index": "5",
"str": "str5",
"cent": "500",
"triplet": "555"
}]
键值对是动态从服务器获取的,我事先不知道要期望什么数据。对于我使用的绘图库,需要JSON中的值是数字而不是字符串,例如
"index":2
而不是"index":"2"
。我需要在客户端使用纯JS或jQuery进行此操作。这是我的方法,但似乎无效:
var temp = //some json that I receive
var jsonForChart = jQuery.extend(true, {}, temp);
$.each(temp, function(key, value) {
$.each(value, function(k, v) {
if(!isNaN(v)){
jsonForChart[key][k] = Number(v);
}
});
});