开发者

formatting array into plist

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

alt text http://www.baublet.com/images/array3.png

I need it to have the following format:

alt text http://www.baublet.com/images/array4.png

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 TE开发者_StackOverflow中文版ST

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 http://www.baublet.com/images/array5.png


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜