views:

49

answers:

1

I have a JSON return being formatted and saved into a plist, it looks like the following:

alt text

I need it to have the following format:

alt text

I'm missing something in the saving of the array so I can add the "Rows" below the Root and "Item 0, etc" under the profile, any ideas?

Here is my code:

    NSURL *url = [NSURL URLWithString:@"http://10.0.1.8/~imac/iphone/jsontest.php"];
    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];

    NSLog(jsonreturn); // Look at the console and you can see what the results are


SBJSON *json = [[SBJSON alloc] init];
NSError *error = nil;
rowsArray= [json objectWithString:jsonreturn error:&error];
 //ADDED from COMMENT AS TEST

test = [rowsArray valueForKey:@"member"];

//Saving 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *choiceR = [documentsDirectory stringByAppendingPathComponent:@"FollowingArray"];
NSMutableArray *array = [[NSMutableArray alloc] init];

NSLog(@"string: %@",choiceR);

[array addObject:rowsArray];
[array writeToFile:choiceR atomically:YES];
[array release];

[jsonreturn release];
[json release];

Here is the test plist from the test:

alt text

+1  A: 

so I can add the "Rows" below the Root

Create an NSDictionary with your rowsArray as the value for the key @"Rows". Then save that dictionary instead of the array.

and "Item 0, etc" under the profile

You'll have to grab the profile dictionary for each item and manually convert it to the desired format, then replace the dictionary with the resulting array. This is nothing that can be done in one or two lines but it shouldn't be too difficult. Just grab the keys of the dictionary and for each of them, create a new dictionary with the desired key/value pair, then add all these dicts to an array.

Ole Begemann
I added this and posted the results on the bottom of my question. test = [rowsArray valueForKey:@"member"]; It does get rid of one level, but doesn't substitute Item O with "Rows", ideas? Thanks for your time. I'm a newbie, any good ideas where to find good info on these nested arrays?
Michael Robinson
Turns out that I was saving it as an array instead of a dictionary. This takes care of the 1st level, I'm not sure how to do the second part,"Item 0"
Michael Robinson