简体   繁体   English

解析和剥离json对象中的引号

[英]Parse and strip quotes from json objects

My data 我的资料

var data = [
{"title":"2013-12-09", "calls_in":"345", "calls_out":"100"},
{"title":"2013-12-08", "calls_in":"125", "calls_out":"97"}, 
{"title":"2013-12-07", "calls_in":"85", "calls_out":"2"},
{"title":"2013-12-06", "calls_in":"185", "calls_out":"22"},
{"title":"2013-12-05", "calls_in":"74", "calls_out":"419"}
]

arrives from a PHP script in json format as above. 从PHP脚本以json格式到达,如上所述。 I'm using an API namely 'jChartFX', it's basically a javascript graphing library that makes cool charts on the fly with javascript/jquery. 我正在使用一个名为“ jChartFX”的API,它基本上是一个javascript图形库,可使用javascript / jquery快速生成漂亮的图表。

Currently using the data above the library fails to parse it. 当前使用库上方的数据无法解析它。 It fails due to the quotes around the integer values. 由于整数值两边的引号导致失败。 I need the data to in the format: 我需要将数据格式为:

var data = [
        {"title":"2013-12-09", "calls_in":345, "calls_out":100},
        {"title":"2013-12-08", "calls_in":125, "calls_out":97}, 
        {"title":"2013-12-07", "calls_in":85, "calls_out":2},
        {"title":"2013-12-06", "calls_in":185, "calls_out":22},
        {"title":"2013-12-05", "calls_in":74, "calls_out":419}
    ]

you see, the integers are NOT wrapped in quotes. 您会看到,整数没有用引号引起来。 How can i do this using jQuery or javascript. 我如何使用jQuery或JavaScript做到这一点。

In it's most basic form (working) the full script would look something similar to: 以最基本的形式(有效),完整脚本看起来类似于:

<!DOCTYPE html>
<html>
<head>
 <link rel="stylesheet" type="text/css" href="chartfx.css" />
 <script type="text/javascript" src="js/jchartfx.system.js"></script>
 <script type="text/javascript" src="js/jchartfx.coreBasic.js"></script>  
</head>
<body onload="loadChart()">
<div id="ChartDiv" style="width:600px;height:400px"></div>
<script>

var chart1;

function loadChart(){        

    chart1 = new cfx.Chart();
    chart1.getData().setSeries(2);
    chart1.getAxisY().setMin(0);
    chart1.getAxisY().setMax(700);

    var series1 = chart1.getSeries().getItem(0);
    var series2 = chart1.getSeries().getItem(1);

    series1.setGallery(cfx.Gallery.Lines);
    series2.setGallery(cfx.Gallery.Lines);

    var data = [
        {"title":"2013-12-09", "calls_in":345, "calls_out":100},
        {"title":"2013-12-08", "calls_in":125, "calls_out":97}, 
        {"title":"2013-12-07", "calls_in":85, "calls_out":2},
        {"title":"2013-12-06", "calls_in":185, "calls_out":22},
        {"title":"2013-12-05", "calls_in":74, "calls_out":419}
    ]

    console.log(data);
        chart1.setDataSource(data);
        var divHolder = document.getElementById('ChartDiv');
    chart1.create(divHolder);            

  }

</script>

</body>
</html>

Option 1: just call parseInt() in the JavaScript on each integer value before passing the data to jChartFX. 选项1:在将数据传递给jChartFX之前,只需在JavaScript中对每个整数值调用parseInt()

Option 2: If you can modify the PHP script, encode your JSON like this: json_encode($arrayContainingTheData, JSON_NUMERIC_CHECK ); 选项2:如果可以修改PHP脚本,则对JSON进行如下编码: json_encode($arrayContainingTheData, JSON_NUMERIC_CHECK );

Example of option 2: 选项2的示例:

$x = array( 'foo' => "1", 'bar' => "2");
echo json_encode($x, JSON_NUMERIC_CHECK  );

Result: {"foo":1,"bar":2} 结果: {"foo":1,"bar":2}

More info: 更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM