我如何在 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]]
我要做的就是循环遍历该数据结构中的“作业”并在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯......问题并没有说数据来自不同的域。您可以使用 JSON 解析器(像这个)来解析 JSON 结果:
然后传递
result
到你的函数中。结果将包含一个标准的 JavaScript 数据结构,您可以使用它来访问您的数据。例如,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:
then pass
result
into your function. Result will contain a standard JavaScript data structure that you can use to access your data. For example,使用 JSONP。
Use JSONP.