将数组格式化为 plist
我有一个 JSON 返回被格式化并保存到 plist 中,它看起来如下所示:
alt text http ://www.baublet.com/images/array3.png
我需要它具有以下格式:
alt text http://www.baublet.com/images/array4.png
我在保存数组时遗漏了一些内容,因此我可以在根下面添加“行”和“个人资料下的“项目 0 等”,有什么想法吗?
这是我的代码:
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];
这是测试中的测试 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 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用您的
rowsArray
作为键@"Rows"
的值创建一个 NSDictionary。然后保存该字典而不是数组。您必须获取每个项目的
profile
字典,并手动将其转换为所需的格式,然后将字典替换为结果数组。这不是一两行就能完成的事情,但应该不会太困难。只需获取字典的键,并为每个键创建一个具有所需键/值对的新字典,然后将所有这些字典添加到一个数组中。Create an NSDictionary with your
rowsArray
as the value for the key@"Rows"
. Then save that dictionary instead of the array.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.