0

我有一个来自 Weather API 的 json 响应。我想获取特定属性的数据,这是响应:

 {
    "response": {
        "version": "0.1",
        "termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
        "features": {
            "forecast": 1
        }
    },
    "forecast": {
        "txt_forecast": {
            "date": "2:00 PM PDT",
            "forecastday": [
                {
                    "period": 0,
                    "icon": "partlycloudy",
                    "icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
                    "title": "Tuesday",
                    "fcttext": "Partly cloudy in the morning, then clear. High of 68F. Breezy. Winds from the West at 10 to 25 mph.",
                    "fcttext_metric": "Partly cloudy in the morning, then clear. High of 20C. Windy. Winds from the West at 20 to 35 km/h.",
                    "pop": "0"
                },
                {
                    "period": 1,
                    "icon": "partlycloudy",
                    "icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
                    "title": "Tuesday Night",
                    "fcttext": "Mostly cloudy. Fog overnight. Low of 50F. Winds from the WSW at 5 to 15 mph.",
                    "fcttext_metric": "Mostly cloudy. Fog overnight. Low of 10C. Breezy. Winds from the WSW at 10 to 20 km/h.",
                    "pop": "0"
                }......

我想得到预测: txt_forecast : fcttext我想从上面的响应中得到“fcttext”。我可以使用下面的代码得到日期。

 var date = parsed_json['forecast']['txt_forecast']['date'];

请帮助我如何从响应中获取“fcttext”。

4

4 回答 4

1

txt_forecast.forecastday是一个数组。数组的每个元素都是一个包含 的实例的对象fcttext。如果你想获得第一个元素,你可以这样做:

parsed_json['forecast']['txt_forecast']['forecastday'][0]['fcctext']
于 2013-11-15T06:19:51.127 回答
1

尝试这个:

for(var forcast_arr_index in parsed_json['forecast']['txt_forecast']['forcastday'])
    console.log(parsed_json['forecast']['txt_forecast']['forcastday'][forcast_arr_index]['fcctest']);

小心报价。

于 2013-11-15T06:23:49.747 回答
0

尝试这样的事情

        var obj = jQuery.parseJSON(json);
        var forecastday_arr = obj.forecast.txt_forecast.forecastday;
        jQuery(forecastday_arr).each(function(i){
            var forecastday_obj = forecastday_arr[i];
            console.log(forecastday_obj);
        });
于 2013-11-15T06:41:29.200 回答
0

您可能想做这样的事情,因为fcttextforecastday内,而forecastdayArray...

$.each(parsed_json.forecast.txt_forecast.forecastday, function(key, object){
     console.log(object.fcttext);
     alert(object.fcttext);
});

这将遍历forecastday对象并告诉您它的fcttext的值。

由于使用$,ajax;标签,可以使用Array.forEachor for

链接

http://api.jquery.com/each      -    jQuery.each文档

https://developer.mozilla.org   -     Array.forEach文档

https://developer.mozilla.org   -     for文档

于 2013-11-15T07:11:55.417 回答