简体   繁体   English

如何解析JSON数组

[英]How to parse a JSON array

new here and hit a roadblock, been searching but can't find the answer with my skill set. 新功能并遇到障碍,一直在搜索,但找不到我所具备的技能答案。 Task is pretty simple, I want to parse this http://data.sparkfun.com/output/AJ2p4r8Owvt1MyV8q9MV.json which is from a weather station. 任务非常简单,我想解析来自气象站的http://data.sparkfun.com/output/AJ2p4r8Owvt1MyV8q9MV.json I have used the W3C tutorial but just can't seem to parse this file, but http://json.parser.online.fr has no problem. 我使用过W3C教程,但似乎无法解析此文件,但是http://json.parser.online.fr没问题。 All the looping parse examples just give me alert after alert. 所有循环分析示例只是让我一个又一个警报。

All I want is the ability to select temp[0] (out of god knows how many) for example via javascript and have it display on a website. 我想要的就是能够例如通过javascript选择temp [0](天生就知道有多少个)并将其显示在网站上。 I'm really lost, tried searching and if I've missed the goldmine then my bad. 我真的迷路了,尝试搜寻,如果我错过了金矿,那我就不好了。 Thanks! 谢谢!

Example code 范例程式码

var text = '[{"humidity":"42.8000","stationtime":"2014-07-06 19:43:52","temp":"23.3000","timestamp":"2014-07-06T09:44:07.918Z"},{"humidity":"‌​43.0000","stationtime":"2014-07-06 19:42:57","temp":"23.2000","timestamp":"2014-07-06T09:42:22.003Z"},{"humidity":"‌​43.2000","stationtime":"2014-07-06 19:42:36","temp":"23.3000","timestamp":"2014-07-06T09:42:51.737Z"}]';  
var obj = JSON.parse(text); 
document.getElementById("demo").innerHTML = obj.temp[0]; 

First, you need to parse the incoming string as below: 首先,您需要解析传入的字符串,如下所示:

temp_arr = JSON.parse(json_string);

Just loop over the temp_arr array, and in each iteration of loop you'll have one object (tobj). 只需在temp_arr数组上循环,在循环的每次迭代中,您将有一个对象(tobj)。 For example, like this: 例如,像这样:

{"humidity":"40.9000","stationtime":"2014-07-06 21:21:03","temp":"22.6000","timestamp":"2014-07-06T11:20:27.231Z"}

All you have to do is, access it like tobj.temp and use it to display on page. 您所要做的就是像tobj.temp一样访问它,并用它显示在页面上。

I have written a jquery implementation at: http://jsfiddle.net/DNH5n/2/ 我已经在以下网址写了一个jQuery实现: http//jsfiddle.net/DNH5n/2/

Jquery makes working with JSONP much easier heres an example ( http://jsfiddle.net/icodeforlove/9mBsr/ ) jQuery使使用JSONP更加容易,这里有一个示例( http://jsfiddle.net/icodeforlove/9mBsr/

$.getJSON('http://data.sparkfun.com/output/AJ2p4r8Owvt1MyV8q9MV.json?callback=?', function (data) {
    data.forEach(function (item) {
        $('body').append(JSON.stringify(item)); 
    });
})

update again 再次更新

heres another example using your code ( http://jsfiddle.net/icodeforlove/9mBsr/2/ ) 这是使用您的代码的另一个示例( http://jsfiddle.net/icodeforlove/9mBsr/2/

var text = '[{"humidity":"42.8000","stationtime":"2014-07-06 19:43:52","temp":"23.3000","timestamp":"2014-07-06T09:44:07.918Z"},{"humidity":"‌43.0000","stationtime":"2014-07-06 19:42:57","temp":"23.2000","timestamp":"2014-07-06T09:42:22.003Z"},{"humidity":"‌43.2000","stationtime":"2014-07-06 19:42:36","temp":"23.3000","timestamp":"2014-07-06T09:42:51.737Z"}]';

var obj = JSON.parse(text);

document.getElementById("demo").innerHTML = obj[0].temp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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