Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing a JSON array into a NSDictionary
    text
    copied!<p>I'm working with the Weather Underground API to make an app and I've hit a snag while parsing the block relating to severe alerts. The JSON uses key-value pairs that have sub key value pairs -- which haven't been a problem for me, as I can make subsequent NSDictionaries out of those -- but the entry for severe alerts has proven problematic. See below:</p> <pre><code>"alerts": [ { "type": "WAT", "description": "Flash Flood Watch", "date": "3:13 PM EDT on April 28, 2012", "date_epoch": "1335640380", "expires": "8:00 AM EDT on April 29, 2012", "expires_epoch": "1335700800", "message": "\u000A...Flash Flood Watch in effect through Sunday morning...\u000A\u000AThe National Weather Service in Charleston has issued a\u000A\u000A* Flash Flood Watch for portions of northeast Kentucky... (Note: I trimmed this for length's sake), "phenomena": "FF", "significance": "A" } ] </code></pre> <p>The "alerts" pair differs from others I've been able to parse because it has this <strong>[ ] bracket</strong> surrounding the sub-values and I'm not sure how to clear it so I can access the subvalues. In the other examples I've been able to parse, it only has the <strong>{ } brackets</strong>, and not both the { } and [ ] brackets. For reference, the brackets are always present -- even when there are no severe weather alerts... in that instance the "alerts" pair returns the brackets [ ] with no sub-pairs present.</p> <p>Is there a way I can remove the [ ] brackets from the NSDictionary, or otherwise ignore them? Any advice would be appreciated!</p> <hr> <p>For reference and troubleshooting help, here's how I'm parsing the rest of the JSON document successfully:</p> <p>1) Create an NSDictionary from the raw JSON</p> <pre><code>//Process Weather Call NSError* error; NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&amp;error]; </code></pre> <p>2) Create subsequent dictionaries for nested json pairs</p> <pre><code>NSDictionary *current_observation = [json objectForKey:@"current_observation"]; </code></pre> <p>3) Assign values</p> <pre><code>NSString* weather; weather = [current_observation objectForKey:@"weather"]; </code></pre> <p>So the end result would be a string that says "Partly Cloudy" or something, along with numerous related weather values that I haven't shown. These parse successfully because they only have the scope brackets { }, and not the [ ] brackets.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload