如何遍历json响应? [英] How to walk through json response?

查看:31
本文介绍了如何遍历json响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 json 响应,我试图通过它来获取天气条件,例如湿度"和temp_C"等.我尝试了一些方法但没有奏效.

I have this json response and I'm trying to walke thought it to get the weather conditions such as "humidity" and "temp_C" and so on. I tried some ways but didn't work.

({ "data" : { "current_condition" : [ { "cloudcover" : "50",
            "humidity" : "44",
            "observation_time" : "12:10 AM",
            "precipMM" : "0.0",
            "pressure" : "1013",
            "temp_C" : "-2",
            "temp_F" : "29",
            "visibility" : "16",
            "weatherCode" : "116",
            "weatherDesc" : [ { "value" : "Partly Cloudy" } ],
            "weatherIconUrl" : [ { "value" : "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png" } ],
            "winddir16Point" : "W",
            "winddirDegree" : "280",
            "windspeedKmph" : "24",
            "windspeedMiles" : "15"
          } ],
      "request" : [ { "query" : "Rochester, United States Of America",
            "type" : "City"
          } ],
      "weather" : [ { "date" : "2012-02-25",
            "precipMM" : "2.2",
            "tempMaxC" : "-1",
            "tempMaxF" : "31",
            "tempMinC" : "-5",
            "tempMinF" : "24",
            "weatherCode" : "116",
            "weatherDesc" : [ { "value" : "Partly Cloudy" } ],
            "weatherIconUrl" : [ { "value" : "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png" } ],
            "winddir16Point" : "W",
            "winddirDegree" : "281",
            "winddirection" : "W",
            "windspeedKmph" : "54",
            "windspeedMiles" : "34"
          } ]
    } })

我试过这些:

$.getJSON(urlFromMyAPI, function (data) {
    alert(data.current_condition.temp_C);
    alert(data.temp_C);
    alert(data[current_condition].temp_C);
    // I also use loop
    for (i = 0; i <= 3; i++) {
        alert(data.current_condition[i])
    }
});
};

推荐答案

我认为您的主要问题是您的数据嵌套在名为 data 的对象中,因此您需要额外的引用级别才能获得在里面.当您像这样设置响应格式时,您可以更轻松地查看所获得的内容,以便您可以更清楚地看到嵌套的对象和数组:

I think your main issue is that your data is nested inside an object named data so you need an extra level of reference to get inside it. It's also a lot easier to see what you've got when you format your response like this so you can see the nested objects and arrays more clearly:

({ "data": { 
    "current_condition": [ 
        {
            "cloudcover": "50", 
            "humidity": "44", 
            "observation_time": "12:10 AM", 
            "precipMM": "0.0", 
            "pressure": "1013", 
            "temp_C": "-2", 
            "temp_F": "29", 
            "visibility": "16", 
            "weatherCode": "116",  
            "weatherDesc": [ 
                {"value": "Partly Cloudy" } 
            ],  
            "weatherIconUrl": [ 
                {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0004_black_low_cloud.png" } 
            ], 
            "winddir16Point": "W", 
            "winddirDegree": "280", 
            "windspeedKmph": "24", 
            "windspeedMiles": "15" 
        } 
    ],  
    "request": [ 
        {"query": "Rochester, United States Of America", "type": "City" } 
    ],  
    "weather": [
        {
            "date": "2012-02-25", 
            "precipMM": "2.2", 
            "tempMaxC": "-1", 
            "tempMaxF": "31", 
            "tempMinC": "-5", 
            "tempMinF": "24", 
            "weatherCode": "116",  
            "weatherDesc": [ 
                {"value": "Partly Cloudy" } 
            ],  
            "weatherIconUrl": [
                {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" } 
            ], 
            "winddir16Point": "W", 
            "winddirDegree": "281", 
            "winddirection": "W", 
            "windspeedKmph": "54", 
            "windspeedMiles": "34" 
        } 
    ] 
}
})

也就是说,如果您想获得当前条件 temp_C,它会是这样的(注意我更改了匿名函数的参数名称以减少代码的混乱):

That said, if you want to get the current condition temp_C, it would be like this (note I changed the argument name for your anonymous function to make the code less confusing):

$.getJSON( urlFromMyAPI, function(response){
    var temp = response.data.current_condition[0].temp_C;
});

如果你想把 temp 作为一个数字,你可能需要这样做:

If you want the temp as a number, you may need to do this:

$.getJSON( urlFromMyAPI, function(response){
    var temp = parseInt(response.data.current_condition[0].temp_C, 10);
});

这篇关于如何遍历json响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆