0

How can this json be fetched. It has dynamic values in it. Below is json.

{
 Level1: 
  {
  row1:
  {
  1: "on",
  2: "on",
  3: "on",
  4: "on",
  5: "on",
  6: "on",
  7: "on",
  8: "on",
  9: "on",
  10: "on",
  attr: {
  total: "10",
   type: "gold"
   }
   },
   row2: {
   1: "on",
   2: "on",
   3: "on",
   4: "on",
   5: "on",
  6: "on",
   7: "on",
  8: "on",
  9: "on",
  10: "on",
  attr: {
  total: "10",
  type: "gold"
  }
  }
  },
  Level3: {
 row1: {
 1: "on",
 2: "on",
 3: "on",
 4: "on",
 5: "on",
 6: "on",
 7: "on",
 8: "on",
  9: "on",
 10: "on",
 attr: {
 total: "10",
 type: "silver"
 }
 }
 }
 }

In this number of levels, number of rows is dynamic and inside it the seats are also dynamic , how to tackle this i am not getting it. Please guide for the above, i have already spend a day on it. Below is my code what i had tried yet.

  arrLevels = [self.mGetDataDict allKeys];          // getting all levels here in array
  NSLog(@"keys:%@", arrLevels);

  for(int i=0; i<[arrLevels count]; i++)       // trying to get rows in array 
 {
    receiveDataDict = [self.mGetDataDict valueForKey:[arrLevels objectAtIndex:i]];
    [arrRows addObject:[NSString stringWithFormat:@"%d", [receiveDataDict count]]];
    NSLog(@"data:%@", receiveDataDict);
    for(int j=0; j<[receiveDataDict count]; j++)
    {
        NSLog(@"count:%d", [receiveDataDict count]);
        dictRow = [receiveDataDict valueForKey:@"attr"];
        [arrTot addObject:[dictRow valueForKey:@"total"]];
        [arrType addObject:[dictRow valueForKey:@"type"]];
    }
 }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54

2 Answers2

4

Why are you not using the NSJSONSerialization class? I've linked the Apple documentation for you, and now that you know the name of a very useful class you should be able to easily find plenty of tutorials that you can make use out of.

Just feed your data to that and you'll get a NSDictionary object back.

And of course the next challenge will be what to do with all the various Objective-C objects that appear under your new parsed dictionary.

One way to do this is via keyPaths. For example:

NSString * onOrOff = [receiveDataDict valueForKeyPath: @"row1.1"];

Here is a related question with some answers that may help you out.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
2

Step 1: Download JSON Framework for IOS from here, and Add Library files it to your project

Step 2: import #import "JSON.h" in your .h file

Step 3: Get your json Data from Server or Local.

NSData *data = ...Your JSON Data...;
NSString *responseString = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];

// Parse it Using SBJSON
NSDictionary *dictResults = [NSDictionary dictionaryWithDictionary:[responseString JSONValue]];

// Read all Levels
for(NSString *key in [dictResults allKeys])
{
    NSLog(@"Accessing .... %@",key);
    for(NSString *rowKey in dictResults[key])
    {
        NSLog(@"%@",rowKey);
        for(NSString *valueKey in dictResults[key][rowKey])
        {
            NSLog(@"%@ -> %@",valueKey,dictResults[key][rowKey][valueKey]);
        }
        NSLog(@"--------ROW OVER------------\n");            
    }
    NSLog(@"--------------------\n");
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57