简体   繁体   中英

How do i parse this JSON in Javascript

I have the JSON:

{"jobs":[{"name":"PR-BVT","lastBuild":{"result":"SUCCESS","timestamp":1303358462150}},{"name":"PR-DBT-MYSQL","lastBuild":{"result":"SUCCESS","timestamp":1302057202000}},{"name":"PR-DBT-POSTGRESQL","lastBuild":{"result":"SUCCESS","timestamp":1302057202000}},{"name":"PR-FUNCT","lastBuild":{"result":"SUCCESS","timestamp":1303358496244}},{"name":"PR-LOADT","lastBuild":{"result":"SUCCESS","timestamp":1302057202000}},{"name":"PR-MAIN","lastBuild":{"result":"UNSTABLE","timestamp":1303358515506}},{"name":"PR-SECT","lastBuild":{"result":"SUCCESS","timestamp":1302057211000}},{"name":"PR-WEB-BVT","lastBuild":{"result":"SUCCESS","timestamp":1303358471795}},{"name":"praefectus-qa","lastBuild":{"result":"FAILURE","timestamp":1303358496583}},{"name":"praefectus_main","lastBuild":{"result":"FAILURE","timestamp":1303358476843}},{"name":"praefectus_today","lastBuild":{"result":"FAILURE","timestamp":1303358477786}},{"name":"prefectus-qa-1","lastBuild":{"result":"FAILURE","timestamp":1303358480424}},{"name":"pygments","lastBuild":{"result":"SUCCESS","timestamp":1303358482063}},{"name":"test-slave","lastBuild":{"result":"SUCCESS","timestamp":1302057202000}}]}

and this I got from the URL on the browser

http://10.0.1.115:8080/api/json?tree=jobs[name,lastBuild[timestamp,result]]

What I have to do is to loop through the "jobs" in that data structure and add appropriate rows in the drawChart function, which is:

function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('datetime', 'Date');
    data.addColumn('number', 'Builds Passed');
    data.addColumn('string', 'title1');
    data.addColumn('string', 'text1');
    data.addColumn('number', 'Build Failed');
    data.addColumn('string', 'title2');
    data.addColumn('string', 'text2');
    data.addRows([
      [new Date(2011, 1 ,1), 30000, undefined, undefined, 40645, undefined, undefined],
      [new Date(2011, 2 ,2), 14045, undefined, undefined, 20374, undefined, undefined],
      [new Date(2011, 3 ,3), 55022, undefined, undefined, 50766, undefined, undefined],
      [new Date(2011, 4 ,4), 75284, undefined, undefined, 14334, 'Lemco','Build Failed'],
      [new Date(2011, 5 ,5), 41476, 'Build Passed','Tecore', 66467, undefined, undefined],
      [new Date(2011, 6 ,6), 33322, undefined, undefined, 39463, undefined, undefined],
      [new Date(2011, 7 ,6), 33322, undefined, undefined, 39463, undefined, undefined],
      [new Date(2011, 8 ,6), 33322, undefined, undefined, 39463, undefined, undefined],
      [new Date(2011, 9 ,6), 33322, undefined, undefined, 39463, undefined, undefined],
      [new Date(2011, 10 ,6), 33322, undefined, undefined, 39463, undefined, undefined],
      [new Date(2011, 11 ,6), 33322, undefined, undefined, 39463, undefined, undefined],
      [new Date(2011, 12 ,6), 33322, undefined, undefined, 39463, undefined, undefined]
    ]);

any ideas how ?

使用JSONP

Hmm... the question doesn't say that the data is coming from a different domain. You can use a JSON parser ( like this one ) to parse your JSON result:

var jsonString = "/*your json here*/";
var result = JSON.parse(jsonString);

then pass result into your function. Result will contain a standard JavaScript data structure that you can use to access your data. For example,

result.jobs[0].name //contains PR-BVT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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