我如何在Javascript中解析这个JSON

时间:2011-04-21 13:00:44

标签: javascript json

我有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}}]}

这是我从浏览器上的URL

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

我要做的是循环遍历该数据结构中的“jobs”并在drawChart函数中添加适当的行,即:

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]
    ]);

任何想法如何?

2 个答案:

答案 0 :(得分:0)

使用JSONP

答案 1 :(得分:0)

嗯......问题并不是说数据来自不同的域。您可以使用JSON解析器(like this one)来解析您的JSON结果:

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

然后将result传递给您的函数。结果将包含可用于访问数据的标准JavaScript数据结构。例如,

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