【问题标题】:how to get a specific data from json/jsonp result?如何从 json/jsonp 结果中获取特定数据?
【发布时间】:2013-11-28 11:13:45
【问题描述】:

我有一个来自 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"
                }......

我想获得 forecast: txt_forecast : fcttext 我想从上述响应中获得“fcttext”。我可以使用以下代码获取日期。

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

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

【问题讨论】:

    标签: jquery asp.net json jsonp response


    【解决方案1】:

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

    parsed_json['forecast']['txt_forecast']['forecastday'][0]['fcctext']
    

    【讨论】:

    • parsed_json['forecast']['txt_forecast']['forecastday'][0] 返回什么?
    • 我拼错了fcctext。我修正了答案,再试一次。
    【解决方案2】:

    试试这个:

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

    注意引号。

    【讨论】:

      【解决方案3】:

      试试这样的

              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);
              });
      

      【讨论】:

        【解决方案4】:

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

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

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

        使用$,ajax;,因为标签,你可以使用Array.forEachfor

        链接

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

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

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

        【讨论】:

          猜你喜欢
          • 2015-04-23
          • 2021-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-22
          • 2016-03-10
          相关资源
          最近更新 更多