ilhnctn
ilhnctn

Reputation: 2210

How to parse NSDictionary into array of arrays?

here is my data pattern and i want to create an array of sections, in which case, each section will include an array of rows in that section. I looked at the following questions:

and a few other posts but could get no result. Can anyone please guide me how to create an array of sections, and each section shall include its own rows.. Thanks in advance

EDIT I have allready parsed my data, the log result at the pastebin link is an NSDictionary here is how i fetch&parse it:

 NSString*key1=[ result objectForKey:@"key" ];                
         NSString *s1=[@"http://" stringByAppendingString:server.text];
         NSString *s2=[s1 stringByAppendingString:@"/ipad/button.php"];
         NSURL *url2=[NSURL URLWithString:[s2 stringByAppendingString:[@"?key=" stringByAppendingString:key1]]];

        NSData *data2=[NSData dataWithContentsOfURL:url2];
         result2=[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];

The result in pastebin is this: NSLog(@"Result: %@", result2);

Upvotes: 4

Views: 1413

Answers (1)

Mundi
Mundi

Reputation: 80273

Your data is in JSON format, so just use the NSJSONSerialization class to create the array of arrays for you.

NSString *yourData;
NSData *data = [yourData dataUsingEncoding:NSUTF8StringEncoding];
NSArray *arrayOfArrays = [NSJSONSerialization JSONObjectWithData:data 
    options:NSJSONReadingAllowFragments error:nil];

To use your array of sections in a table view, just use the usual table view datasource methods:

// numberOfSectionsInTableView
return [array count];

// numberOfRowsInSection
return [[array objectAtIndex:section] count];

// cellForRowAtIndexPath
cell.textLabel.text = [[[array objectAtIndex:indexPath.section] 
   objectAtIndex:indexPath.row]
      objectForKey:"name"];

Upvotes: 1

Related Questions