最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Need to parse complex JSON - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 2
var 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)
发布评论

评论列表(0)

  1. 暂无评论