我如何在 Javascript 中解析这个 JSON

发布于 2024-11-02 13:40:11 字数 2880 浏览 0 评论 0原文

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

我要做的就是循环遍历该数据结构中的“作业”并在 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]
    ]);

有什么想法吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-11-09 13:40:11

嗯......问题并没有说数据来自不同的域。您可以使用 JSON 解析器(像这个)来解析 JSON 结果:

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

然后传递 result 到你的函数中。结果将包含一个标准的 JavaScript 数据结构,您可以使用它来访问您的数据。例如,

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文