0

私は次のjsonデータを持っています:

var JSONObject2= {
    "weather": {
        "curren_weather": [
            {
                "humidity": "69",
                "pressure": "1012",
                "temp": "70",
                "temp_unit": "f",
                "weather_code": "1",
                "weather_text": "Mostly cloudy",
                "wind": [
                    {
                        "dir": "Not Available",
                        "speed": "0",
                        "wind_unit": "mph"
                    }
                ]
            }
        ],
        "forecast": [
            {
                "date": "2012-08-21",
                "day": [
                    {
                        "weather_code": "0",
                        "weather_text": "Sunny skies",
                        "wind": [
                            {
                                "dir": "NW",
                                "dir_degree": "311",
                                "speed": "7",
                                "wind_unit": "mph"
                            }
                        ]
                    }
                ],
                "day_max_temp": "83",
                "night": [
                    {
                        "weather_code": "0",
                        "weather_text": "Clear skies",
                        "wind": [
                            {
                                "dir": "WNW",
                                "dir_degree": "289",
                                "speed": "7",
                                "wind_unit": "mph"
                            }
                        ]
                    }
                ],
                "night_min_temp": "57",
                "temp_unit": "f"
            },
            {
                "date": "2012-08-22",
                "day": [
                    {
                        "weather_code": "0",
                        "weather_text": "Sunny skies",
                        "wind": [
                            {
                                "dir": "N",
                                "dir_degree": "7",
                                "speed": "4",
                                "wind_unit": "mph"
                            }
                        ]
                    }
                ],
                "day_max_temp": "85",
                "night": [
                    {
                        "weather_code": "0",
                        "weather_text": "Clear skies",
                        "wind": [
                            {
                                "dir": "S",
                                "dir_degree": "176",
                                "speed": "7",
                                "wind_unit": "mph"
                            }
                        ]
                    }
                ],
                "night_min_temp": "63",
                "temp_unit": "f"
            }
        ]
    }
};

非常に単純なjsonデータで、変数を取得する方法を理解できます。上記のようなことで、私には難しいです。

したがって、気温が70の場所にアクセスするには、次のようにコーディングします。var my_temp =Weather.curren_weather.temp//これは機能しません

2つの例と同じように、temp(70)でどのように取得し、wind_degreeが311であるかを言うにはどうすればよいでしょうか。

また、上記のようにjsonオブジェクトを貼り付けることができ、変数にアクセスするために必要な正しいコードを出力するプログラムはありますか?

ありがとう、ジム

4

1 に答える 1

1

オブジェクトはネストされた配列によって複雑になり、現在の天気でネストされた配列が必要な理由は不明です。

このtemp構造で70を達成するには、次のものが必要です。

var temp = JSONObject2.weather.curren_weather[0].temp;

そして、その特定の風の読書のために:

var wind = JSONObject2.weather.forecast[0].day[0].wind[0].dir_degree;

フィールドの場合forecast、ネストされた配列は妥当です。これは、さまざまな日付の予測のリストが提供されているため、データに表されている2番目の日付の日中の風の読み取り値を次のように抽出できるためです。

var wind = JSONObject2.weather.forecast[1].day[0].wind[0].dir_degree;
于 2012-08-21T16:09:58.803 に答える