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

您的对象因嵌套数组而变得复杂,目前尚不清楚为什么当前天气需要嵌套数组。

在给定这种结构的情况下,要获得temp70 分,您需要:

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

对于那个特定的风读数:

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

对于该forecast字段,嵌套数组是合理的,因为您获得了不同日期 m 的预测列表,因此可以使用以下方法提取数据中表示的第二个日期的白天风读数:

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