如何走过json的回应?

时间:2012-02-26 01:08:39

标签: jquery json api

我有这个json的反应,我试图让walke认为它可以获得诸如“湿度”和“temp_C”等天气条件。我尝试了一些方法,但没有奏效。

({ "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])
    }
});
};

4 个答案:

答案 0 :(得分:4)

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

({ "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,它就像这样(注意我更改了你的匿名函数的参数名称,以减少代码的混乱):

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

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

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

答案 1 :(得分:1)

要遍历JSON对象中包含的数组,您需要访问data.data.current_condition

for(i = 0; i <= 3; i++){
   alert(data.data.current_condition[i]);

   var properties = data.data.current_condition[i];

   for(var y in properties)
       alert(properties[y]);
}

http://jsfiddle.net/m7TZx/

答案 2 :(得分:0)

您的代码存在两个问题。

  1. 您的变量称为数据,JSON中的第一件事就是一个名为data的对象。
  2. current_condition是一个对象数组(在Javascript中,方括号[]引用数组,花括号{}引用一个对象),所以在引用temp_C之前必须说current_condition [index]
  3. 我在此示例中将data重命名为json_data以避免混淆:

    $.getJSON( urlFromMyAPI, function(json_data){
        console.log(json_data.data.current_condition[0].temp_C);
    });
    

    如果您有多个current_condition个对象,可以使用for循环来完成它们:

    $.getJSON( urlFromMyAPI, function(json_data){
        var current_conditions = json_data.data.current_condition;
        for(var i=0; i < current_conditions.length; i++) {
            console.log(current_conditions.temp_C);
        }
    });
    

    如果您想以更好的格式查看,可以使用Javascript美化器(例如http://jsbeautifier.org/)。

    您可能会发现console.logalert更有用。大多数浏览器都有控制台,在Google Chrome中,您可以按F12并点击控制台找到它。

答案 3 :(得分:0)

JSON不应该包含在“()”中,除非在打开“(”)之前将其作为带有函数名称的jsonp发送。现在假设您从指向jsonp url的浏览器复制了响应,这是什么粘贴是一个复制错误。

使用$ .each使循环变得非常容易。

$.each( data.data.current_condition[0], function ( key, value){
    console.log( 'Key:', key, ' Value:', value)
})