Modifying NSMutableDictionary from a single index format into nested (array within array)

Posted by Michael Robinson on Stack Overflow See other posts from Stack Overflow or by Michael Robinson
Published on 2010-06-14T05:09:43Z Indexed on 2010/06/17 0:12 UTC
Read the original article Hit count: 345

I need to take the member ID off the top of this and create an array inside that contains the rest of the JSON return. Here is my JSON return.

[{"F_Name_VC":"Frank", "L_Name_VC":"Johnson", "userid":"18", "age":"23", },]

After it is json-deserialized it looks like this: alt text

I need convert it to be transformed into this, with children sub-array: alt text

Someone else posted a similar question but without the fact that it was coming from a JSON deserialization. There were two answers but no conclusion to the question.

(Answer 1):

 NSDictionary *item1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@" member",[NSNumber numberWithInt:3],nil]
                                                   forKeys:[NSArray arrayWithObjects:@"Title",@"View",nil]];

Answer (2):

    NSMutableArray *Rows = [NSMutableArray arrayWithCapacity: 1];

    for (int i = 0; i < 4; ++i) {
    NSMutableArray *theChildren = [NSMutableArray arrayWithCapacity: 1];
          [theChildren addObject: [NSString stringWithFormat: @"tester %d", i]];
    NSString *aTitle = [NSString stringWithFormat: @"Item %d", i];
    NSDictionary *anItem = [NSDictionary dictionaryWithObjectsAndKeys: aTitle, @"Title", theChildren, @"Children"];
   [Rows addObject: anItem];
    }

    NSDictionary *Root = [NSDictionary withObject: Rows andKey: @"Rows"];

Here is my JSON return and save code:

 NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
 NSError *error = nil;

  NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
if (dict)
{       
    rowsArray = [dict objectForKey:@"member"];
    [rowsArray retain];

}

Thanks in advance. Every tutorial on JSON only shows a simple array being returned, never with children..It's driving me crazy trying to figure this out.

© Stack Overflow or respective owner

Related posts about JSON

Related posts about uitableview