-2

Possible Duplicate:
iPhone/iOS JSON parsing tutorial

I have no clue how to parse JSON data from a website.

    {
"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"
        }
      ]
    }
  ],

All that I want to do is store the "temp" and "weather_text" variables as NSStrings to be used in labels or whatever in my application...

The actual request url is something like: http://www.myweather2.com/developer/forecast.ashx?uac=(access-key)&output=json&query=(latitude,longitude)&temp_unit=f

Any thoughts on how to complete this?

Community
  • 1
  • 1
Jon Sullivan
  • 399
  • 2
  • 5
  • 11

1 Answers1

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.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170