42190

complex JSON parsing using Objective-C

{ "Flight1":{ "3":{ "id":"10", "name":"JumboJet1B", "level":"1", "category":"1", "energy":"10", "bonus":"10", "completed":0 }, "4":{ "id":"10", "name":"JumboJet1B", "level":"1", "category":"1", "energy":"10", "bonus":"10", "completed":0 } } }

This was the json output

How can I parse inside the items of 3 and 4, say getting the id, energy and name

Thanks!

Answer1:

If the order inside Flight1 doesn’t matter, the following should work:

NSDictionary *flights = … // result from a JSON parser NSDictionary *flight1 = [flights objectForKey:@"Flight1"]; for (NSString *key in [flight1 allKeys]) { NSDictionary *flight1Entry = [flight1 objectForKey:key]; NSString *entryId = [flight1Entry objectForKey:@"id"]; NSString *entryName = [flight1Entry objectForKey:@"name"]; NSString *entryEnergy = [flight1Entry objectForKey:@"energy"]; … }

Otherwise, if you want the keys sorted according to their numeric value:

NSDictionary *flights = … // result from a JSON parser NSDictionary *flight1 = [flights objectForKey:@"Flight1"]; NSArray *flight1Keys = [[flight1 allKeys] sortedArrayUsingComparator:^(id o1, id o2) { NSInteger i1 = [o1 integerValue]; NSInteger i2 = [o2 integerValue]; NSComparisonResult result; if (i1 > i2) result = NSOrderedDescending; else if (i1 < i2) result = NSOrderedAscending; else result = NSOrderedSame; return result; }]; for (NSString *key in flight1Keys) { NSDictionary *flight1Entry = [flight1 objectForKey:key]; NSString *entryId = [flight1Entry objectForKey:@"id"]; NSString *entryName = [flight1Entry objectForKey:@"name"]; NSString *entryEnergy = [flight1Entry objectForKey:@"energy"]; … }

Answer2:

Assuming that you are using the json framework you could access it like this:

NSDictionary *jsonDict = [jsonString JSONValue]; NSString *id = [[[jsonDict objectForKey:@"Flight1"] objectForKey:@"3"] objectForKey:@"id"];

This assumes alot, so make use of try except blocks or iterate through the different levels.

Recommend

  • Multiple polylines overlapped on Google Maps
  • Cannot insert sentence to database
  • JsonTypeInfo does not serialize when using Jackson with Jersey
  • SparkR and Packages
  • Populating a UITableView in Firebase a Set Number of Cells at a time
  • how to convert a unix timestamp into nsdate in iphone [duplicate]
  • Encoding issue with ASIHttpRequest
  • Why won't my UITableViewCell deselect and update its text?
  • Error Installing Windows 10 development tools for Visual Studio 2015 RC on Windows 8.1
  • Hibernate - Cannot connect to DB
  • why fullscreen functionality is not working in chrome?
  • How can I make a 100% stacked chart with nvd3?
  • Launch powershell script from UWP app with FullTrustProcessLauncher class
  • jinja2 template not found and internal server error
  • iOS 6 dateFromString returns wrong date
  • Create a link to a web page that runs a Javascript function on the page
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • Compress a file with GZipStream while maintaining its meta-data
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • How can I include If-None-Match header in HttpRequestMessage
  • didUpdatePushCredentials not get called
  • Build Successful but not running on simulator
  • Trouble saving JSON data for use by multiple UIViews [duplicate]
  • Parse a date string in a specific locale (not timezone!)
  • How to change the font size of a single index for UISegmentedControl?
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • Play WS (2.2.1): post/put large request
  • Display issues when we change from one jquery mobile page to another in firefox
  • Different response to non-authenticated users and AJAX calls
  • Do I've to free mysql result after storing it?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • R: gsub and capture
  • php design question - will a Helper help here?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • AngularJs get employee from factory
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Is it possible to post an object from jquery to bottle.py?
  • How to load view controller without button in storyboard?