将json字符串值转换为数字

时间:2015-12-01 06:54:36

标签: javascript jquery

我有一个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中的值是数字而不是字符串viz。 "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);
        }
    });
});

4 个答案:

答案 0 :(得分:5)

类似于this(其中objects是一个对象数组):

的JavaScript

for(var i = 0; i < objects.length; i++){
    var obj = objects[i];
    for(var prop in obj){
        if(obj.hasOwnProperty(prop) && obj[prop] !== null && !isNaN(obj[prop])){
            obj[prop] = +obj[prop];   
        }
    }
}

console.log(JSON.stringify(objects, null, 2));

最后一行将打印出来:

[
  {
    "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
  }
]

答案 1 :(得分:1)

试试这个。我还没有对它进行测试但是应该可以工作。

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(parseInt(v))){
            jsonForChart[key][k] = parseInt(v);
        }else{
            jsonForChart[key][k] = v;
        }
    });
});

答案 2 :(得分:1)

试试这个

// Iterate thorugh the array
[].forEach.call(x, function(inst, i){
    // Iterate through all the keys
    [].forEach.call(Object.keys(inst), function(y){
        // Check if string is Numerical string
        if(!isNaN(x[i][y]))
            //Convert to numerical value
            x[i][y] = +x[i][y];
    });

});

console.log(x);

Live FIddle

答案 3 :(得分:1)

您无需检查该值是否为数字:

&#13;
&#13;
var temp = [{
  "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"
}];

var jsonForChart = jQuery.extend(true, {}, temp);
$.each(temp, function(key, value) {
  $.each(value, function(k, v) {
    // if the value can be parsed to int, it will be OR the value remains untouched
    jsonForChart[key][k] = +v || jsonForChart[key][k];
  });
});

document.write("<pre>" + JSON.stringify(jsonForChart, null, 3) + "</pre>");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

你得到一个大对象,但它是你期望看到的行var jsonForChart = jQuery.extend(true, {}, temp);

相关问题