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

それ以外の場合、たとえば jQuery$.ajax()成功コールバックのパラメーターとしてこの JSON を取得し、それが既にオブジェクトである場合は、 を呼び出す必要はなく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 に答える