Parsing a JSON array into a NSDictionary
Solution 1
The brackets means the Json data there are in an array. You can parse it as following
NSArray *alertArray = [json objectForKey:@"alerts"];
now you should loop through all alerts and parse them (in your case it's only 1, but it could be more in another json string):
//parse each alert
for (NSDictionary *alert in alertArray ){
NSString* description = [alert objectForKey:@"description"];
//etc...
}
Solution 2
Okay, I got it working -- and I wanted to provide an example here because I ended up having to build on the advice @Lefteris gave to get it working.
I ended up having to pass the json array first as an NSArray, and then I converted that into an NSDictionary with the first element of the array. Everything afterwards worked as @Lefteris described.
So, in the end, here's what I've got:
NSArray *alerts = [json objectForKey:@"alerts"];
NSDictionary *alertDict = [[NSDictionary alloc] init];
//Check that no alerts exist to prevent crashing
if([alerts count] < 1) {
NSLog(@"No Alerts Here!");
type = nil;
...
}
else //Populate fields
{
alertDict = [alerts objectAtIndex:0];
for (NSDictionary *alert in alertDict)
{
NSLog(@"Printing alert!");
type = [alertDict objectForKey:@"type"];
...
}
}
This got me up and running with a single array iterate -- going on I expect I can simply iterate through the array since I know the count and handle any additional alerts. Thanks again for the help!
Anthony Neace
Links: http://www.swcity.net http://www.reddit.com/u/hyperanthony
Updated on August 24, 2020Comments
-
Anthony Neace over 3 years
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:
"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" } ]
The "alerts" pair differs from others I've been able to parse because it has this [ ] bracket 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 { } brackets, 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.
Is there a way I can remove the [ ] brackets from the NSDictionary, or otherwise ignore them? Any advice would be appreciated!
For reference and troubleshooting help, here's how I'm parsing the rest of the JSON document successfully:
1) Create an NSDictionary from the raw JSON
//Process Weather Call NSError* error; NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
2) Create subsequent dictionaries for nested json pairs
NSDictionary *current_observation = [json objectForKey:@"current_observation"];
3) Assign values
NSString* weather; weather = [current_observation objectForKey:@"weather"];
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.
-
Anthony Neace about 12 yearsOf course, an array! Those brackets should have given it away... I think I've been working too long today. :p Anyway that also makes sense in the context of the alerts -- there can be more than one alert at a given time, so there should be some way to handle that in the API. I'll give that a try -- thanks for your help!
-
Khay almost 10 yearsThis code is still now working , i tried it with your example itself
-
Anthony Neace almost 10 years@Khay Sorry? Worked fine for me at the time. It seems suspect to me that nobody has pointed out a problem with 10k views and 2 years passed. Are you using the same version of iOS and the same (or comparable) data I was? Also, review the edit history of Lefteris' answer -- the code changed there last year so something may very well have changed with newer versions. I really couldn't tell you as I don't work with iOS anymore.