将数组格式化为 plist

发布于 2024-09-06 19:29:16 字数 1534 浏览 0 评论 0原文

我有一个 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:

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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路黄昏 2024-09-13 19:29:16

这样我就可以在根下面添加“行”

使用您的 rowsArray 作为键 @"Rows" 的值创建一个 NSDictionary。然后保存该字典而不是数组。

以及个人资料下的“项目 0 等”

您必须获取每个项目的 profile 字典,并手动将其转换为所需的格式,然后将字典替换为结果数组。这不是一两行就能完成的事情,但应该不会太困难。只需获取字典的键,并为每个键创建一个具有所需键/值对的新字典,然后将所有这些字典添加到一个数组中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文