首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON @attributes

JSON @attributes
EN

Stack Overflow用户
提问于 2012-07-03 18:24:49
回答 1查看 11.3K关注 0票数 2

我很难理解如何读取包含"@attributes“的JSON对象。

Javascript

代码语言:javascript
复制
$.ajax({
    type: "GET",
    dataType: 'json',
    url: "http://..../script/weather.php?r="+req,
    success: function(data){
       alert(data.weather.forecast_information.city[0].data)
    }
});

JSON响应:

代码语言:javascript
复制
{
    "@attributes": {
        "version": "1"
    },
    "weather": {
        "@attributes": {
            "module_id": "0",
            "tab_id": "0",
            "mobile_row": "0",
            "mobile_zipped": "1",
            "row": "0",
            "section": "0"
        },
        "forecast_information": {
            "city": {
                "@attributes": {
                    "data": "Kreuzlingen, Thurgovia"
                }
            },
            "postal_code": {
                "@attributes": {
                    "data": "kreuzlingen"
                }
            },
            "latitude_e6": {
                "@attributes": {
                    "data": ""
                }
            },
            "longitude_e6": {
                "@attributes": {
                    "data": ""
                }
            },
            "forecast_date": {
                "@attributes": {
                    "data": "2012-07-03"
                }
            },
            "current_date_time": {
                "@attributes": {
                    "data": "1970-01-01 00:00:00 +0000"
                }
            },
            "unit_system": {
                "@attributes": {
                    "data": "US"
                }
            }
        },
        "current_conditions": {
            "condition": {
                "@attributes": {
                    "data": "Cloudy"
                }
            },
            "temp_f": {
                "@attributes": {
                    "data": "70"
                }
            },
            "temp_c": {
                "@attributes": {
                    "data": "21"
                }
            },
            "humidity": {
                "@attributes": {
                    "data": "Humidity: 68%"
                }
            },
            "icon": {
                "@attributes": {
                    "data": "/ig/images/weather/cloudy.gif"
                }
            },
            "wind_condition": {
                "@attributes": {
                    "data": "Wind: N at 0 mph"
                }
            }
        },
        "forecast_conditions": [
            {
                "day_of_week": {
                    "@attributes": {
                        "data": "Tue"
                    }
                },
                "low": {
                    "@attributes": {
                        "data": "55"
                    }
                },
                "high": {
                    "@attributes": {
                        "data": "72"
                    }
                },
                "icon": {
                    "@attributes": {
                        "data": "/ig/images/weather/thunderstorm.gif"
                    }
                },
                "condition": {
                    "@attributes": {
                        "data": "Thunderstorm"
                    }
                }
            },
            {
                "day_of_week": {
                    "@attributes": {
                        "data": "Wed"
                    }
                },
                "low": {
                    "@attributes": {
                        "data": "66"
                    }
                },
                "high": {
                    "@attributes": {
                        "data": "79"
                    }
                },
                "icon": {
                    "@attributes": {
                        "data": "/ig/images/weather/chance_of_storm.gif"
                    }
                },
                "condition": {
                    "@attributes": {
                        "data": "Chance of Storm"
                    }
                }
            },
            {
                "day_of_week": {
                    "@attributes": {
                        "data": "Thu"
                    }
                },
                "low": {
                    "@attributes": {
                        "data": "61"
                    }
                },
                "high": {
                    "@attributes": {
                        "data": "77"
                    }
                },
                "icon": {
                    "@attributes": {
                        "data": "/ig/images/weather/chance_of_storm.gif"
                    }
                },
                "condition": {
                    "@attributes": {
                        "data": "Chance of Storm"
                    }
                }
            },
            {
                "day_of_week": {
                    "@attributes": {
                        "data": "Fri"
                    }
                },
                "low": {
                    "@attributes": {
                        "data": "63"
                    }
                },
                "high": {
                    "@attributes": {
                        "data": "79"
                    }
                },
                "icon": {
                    "@attributes": {
                        "data": "/ig/images/weather/chance_of_rain.gif"
                    }
                },
                "condition": {
                    "@attributes": {
                        "data": "Chance of Rain"
                    }
                }
            }
        ]
    }
}

错误(Chrome);

未定义的TypeError:无法读取未定义的属性“数据”

的问题是,如何使“克鲁兹林根,瑟戈维亚”处于戒备状态?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-03 18:28:10

像这样:

代码语言:javascript
复制
alert(data.weather.forecast_information.city["@attributes"].data)
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11316923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档