0

我想解析 Weather API (URL) 城市名称、温度等。

我的 JSON 数据如下:

{
    "data": {
        "current_condition": [{
            "cloudcover": "25",
            "humidity": "70",
            "observation_time": "04:21 PM",
            "precipMM": "0.3",
            "pressure": "1007",
            "temp_C": "30",
            "temp_F": "86",
            "visibility": "4",
            "weatherCode": "113",
            "weatherDesc": [{
                "value": "Clear"}],
            "weatherIconUrl": [{
                "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png"}],
            "winddir16Point": "S",
            "winddirDegree": "180",
            "windspeedKmph": "7",
            "windspeedMiles": "4"}],
        "request": [{
            "query": "Ahmedabad, India",
            "type": "City"}],
        "weather": [{
            "date": "2012-09-18",
            "precipMM": "2.1",
            "tempMaxC": "32",
            "tempMaxF": "89",
            "tempMinC": "25",
            "tempMinF": "76",
            "weatherCode": "176",
            "weatherDesc": [{
                "value": "Patchy rain nearby"}],
            "weatherIconUrl": [{
                "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png"}],
            "winddir16Point": "SSW",
            "winddirDegree": "203",
            "winddirection": "SSW",
            "windspeedKmph": "12",
            "windspeedMiles": "8"},
        {
            "date": "2012-09-19",
            "precipMM": "3.4",
            "tempMaxC": "32",
            "tempMaxF": "89",
            "tempMinC": "25",
            "tempMinF": "76",
            "weatherCode": "176",
            "weatherDesc": [{
                "value": "Patchy rain nearby"}],
            "weatherIconUrl": [{
                "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png"}],
            "winddir16Point": "SW",
            "winddirDegree": "223",
            "winddirection": "SW",
            "windspeedKmph": "12",
            "windspeedMiles": "7"}]
    }
}​

如何解析这些数据并获取城市名称和温度..我不知道..提前谢谢。

===============输出========================

我想获取这样的数据并在文本框上设置

Date        2012-09-18    2012-09-19

tempMaxC    32               32
tempMinC    25               25

tempMaxF    89               89
tempMinF    76               76
4

1 回答 1

3

如果您已将此 JSON 作为字符串检索,则将此字符串传递给JSON.parse()*,然后访问检索到的值作为常规 JavaScript 对象:

var jsonStr = '{ "data": { "current_condition": [ {"cloudcover": "25", "humidity": "70", "observation_time": "04:21 PM", "precipMM": "0.3", "pressure": "1007", "temp_C": "30", "temp_F": "86", "visibility": "4", "weatherCode": "113",  "weatherDesc": [ {"value": "Clear" } ],  "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png" } ], "winddir16Point": "S", "winddirDegree": "180", "windspeedKmph": "7", "windspeedMiles": "4" } ],  "request": [ {"query": "Ahmedabad, India", "type": "City" } ],  "weather": [ {"date": "2012-09-18", "precipMM": "2.1", "tempMaxC": "32", "tempMaxF": "89", "tempMinC": "25", "tempMinF": "76", "weatherCode": "176",  "weatherDesc": [ {"value": "Patchy rain nearby" } ],  "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "SSW", "winddirDegree": "203", "winddirection": "SSW", "windspeedKmph": "12", "windspeedMiles": "8" }, {"date": "2012-09-19", "precipMM": "3.4", "tempMaxC": "32", "tempMaxF": "89", "tempMinC": "25", "tempMinF": "76", "weatherCode": "176",  "weatherDesc": [ {"value": "Patchy rain nearby" } ],  "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "SW", "winddirDegree": "223", "winddirection": "SW", "windspeedKmph": "12", "windspeedMiles": "7" } ] }}',
    jsonObj = JSON.parse(jsonStr);
    console.log(jsonObj.data.current_condition[0].temp_F);

否则,如果您已检索此 JSON,例如作为某些 jQuery$.ajax()成功回调的参数并且它已经是一个对象,则无需调用JSON.parse(),而只需直接检索对象的值:

$.getJSON("http://example.com/weather.json", function(jsonObj) {
    // The response string is already parsed with $.parseJSON(),
    // so you don't need to parse it yourself.
    // Therefore just go ahead and access the properties of JavaScript object.
    console.log(jsonObj.data.current_condition[0].temp_F);
});

*如果您打算支持不支持 JSON.parse/stringify 的旧版浏览器(例如 IE7),则需要包含JSON 库

更新:

特定案例的演示

于 2012-09-18T19:08:26.827 回答