-2

可能重复:
iPhone/iOS JSON 解析教程

我不知道如何从网站解析 JSON 数据。

    {
"weather": {
  "curren_weather": [
    {
      "humidity": "54",
      "pressure": "1011",
      "temp": "50",
      "temp_unit": "f",
      "weather_code": "1",
      "weather_text": "Partly cloudy",
      "wind": [
        {
          "dir": "W",
          "speed": "9",
          "wind_unit": "kph"
        }
      ]
    }
  ],

我想要做的就是将“temp”和“weather_text”变量存储为 NSStrings 以用于标签或我的应用程序中的任何内容......

实际的请求 url 类似于:http ://www.myweather2.com/developer/forecast.ashx?uac=(access-key)&output=json&query=(latitude,longitude)&temp_unit=f

关于如何完成这个的任何想法?

4

1 回答 1

1

I hope that's not the whole thing-- there should be a couple of extra }s at the end, or it isn't valid JSON.

Parse JSON using NSJSONSerialization's JSONObjectWithData:options:error: method. For the JSON above, it will give you an NSDictionary. You can then look up values using NSDictionary methods or key-value coding. If you fix the JSON and parse it this way, you'd use a key path like weather.curren_weather.temp (is it really curren_weather and not current_weather?) to get the temperature.

于 2013-01-31T16:49:02.340 回答