admin管理员组文章数量:1336595
I would like to parse a plex JSON structure but struggling with it as it is not straight a forward JSON. Here is the sample:
[
{
"target":"collectd.matrix.oracle.avg_resp_time",
"datapoints":[
[8.0, 1365158480],
[null, 1365158490],
[null, 1365158500],
[null, 1365158510],
[null, 1365158520],
[null, 1365158530],
[8.0, 1365158540],
[null, 1365158550],
[null, 1365158560],
[null, 1365158570],
[null, 1365158580],
[null, 1365158590],
[8.0, 1365158600],
[null, 1365158610],
[null, 1365158620],
[null, 1365158630],
[null, 1365158640],
[null, 1365158650],
[8.0, 1365158660],
[null, 1365158670],
[null, 1365158680],
[null, 1365158690],
[null, 1365158700],
[null, 1365158710],
[null, 1365158720],
[null, 1365158730],
[null, 1365158740],
[null, 1365158750],
[null, 1365158760],
[null, 1365158770]
]
}
]
I want to capture the value of each field like eg:X=8.0,Y=1365158540
and need some help or logic to parse this.
Thanks, sohan
I would like to parse a plex JSON structure but struggling with it as it is not straight a forward JSON. Here is the sample:
[
{
"target":"collectd.matrix.oracle.avg_resp_time",
"datapoints":[
[8.0, 1365158480],
[null, 1365158490],
[null, 1365158500],
[null, 1365158510],
[null, 1365158520],
[null, 1365158530],
[8.0, 1365158540],
[null, 1365158550],
[null, 1365158560],
[null, 1365158570],
[null, 1365158580],
[null, 1365158590],
[8.0, 1365158600],
[null, 1365158610],
[null, 1365158620],
[null, 1365158630],
[null, 1365158640],
[null, 1365158650],
[8.0, 1365158660],
[null, 1365158670],
[null, 1365158680],
[null, 1365158690],
[null, 1365158700],
[null, 1365158710],
[null, 1365158720],
[null, 1365158730],
[null, 1365158740],
[null, 1365158750],
[null, 1365158760],
[null, 1365158770]
]
}
]
I want to capture the value of each field like eg:X=8.0,Y=1365158540
and need some help or logic to parse this.
Thanks, sohan
Share Improve this question edited Aug 6, 2020 at 12:50 Andreas 5,60910 gold badges47 silver badges55 bronze badges asked Apr 5, 2013 at 10:54 SohanSohan 6,8196 gold badges40 silver badges58 bronze badges 5- 4 That is straight forward JSON, any JSON parser will be able to parse it. – Quentin Commented Apr 5, 2013 at 10:55
- jslint. approves the json provided. fiddle with console output of the parsed object – Imperative Commented Apr 5, 2013 at 10:56
- I am not an UI,JS developer but thee is some requrement so struggling to built the logic arount it – Sohan Commented Apr 5, 2013 at 10:57
- what have you tried ? mattgemmell./2008/12/08/what-have-you-tried – mpm Commented Apr 5, 2013 at 10:58
- 1 The question is a good one. It is not a simple array of objects, like all the tutorials show how to loop through. There are a different structures in the array. – Andrew Koper Commented Sep 27, 2018 at 0:20
3 Answers
Reset to default 2var jsonData = JSON.parse(data)
where
data = '[{"target": "collectd.matrix.oracle.avg_resp_time", "datapoints": [[8.0, 1365158480], [null, 1365158490], [null, 1365158500], [null, 1365158510], [null, 1365158520], [null, 1365158530], [8.0, 1365158540], [null, 1365158550], [null, 1365158560], [null, 1365158570], [null, 1365158580], [null, 1365158590], [8.0, 1365158600], [null, 1365158610], [null, 1365158620], [null, 1365158630], [null, 1365158640], [null, 1365158650], [8.0, 1365158660], [null, 1365158670], [null, 1365158680], [null, 1365158690], [null, 1365158700], [null, 1365158710], [null, 1365158720], [null, 1365158730], [null, 1365158740], [null, 1365158750], [null, 1365158760], [null, 1365158770]]}]';
jsonData[0]['datapoints']
is the array of all the datapoints
Reference for JSON.parse
The native JSON.parse()
should work just fine. Use json2.js for backwards patibility in older browsers. Here is an example:
var data = JSON.parse(yourJsonGoesHere),
datapoints = data[0].datapoints,
i;
for (i = 0; i < datapoints.length; ++i) {
console.log('x:' + datapoints[i][0] + ', y:' + datapoints[i][1]);
}
you can just add datatype:json
in your ajax call if you are getting response via ajax
OR
you can use http://api.jquery./jQuery.parseJSON/
var obj = jQuery.parseJSON(jsonString)
本文标签: javascriptNeed to parse complex JSONStack Overflow
版权声明:本文标题:javascript - Need to parse complex JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742410447a2469678.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论