如何以下列格式将此json保存为Plist?

时间:2013-12-15 20:44:27

标签: ios nsarray nsdictionary plist

您好我有一个类似于以下结构的JSON

{
   "taxi":[
      {
         "id":"173",
         "taxiName":"texi cabs",
         "taxiDescription":"test desc",
         "contactNumber":[
            {
               "Number":"vodafoen,4686565635"
            }
         ]
      },
      {
         "id":"172",
         "taxiName":"Blue Moon",
         "taxiDescription":"test desc",
         "contactNumber":[
            {
               "Number":"vodafone,45686532"
            }
         ]
      },
      {
         "id":"174",
         "taxiName":"yellow cabs",
         "taxiDescription":"test",
         "contactNumber":[
            {
               "Number":"airvoice,657865"
            },
            {
               "Number":"simmens,643437547457"
            },
            {
               "Number":"tescom,96831635365"
            }
         ]
      }
   ]
}

Link

从这个iam尝试将每个元素的“contactNumber”保存到新的plist中。请帮帮我。

所以最终的plist看起来很像 所有这3个要素的联系清单

我试过这个

   paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *taxiPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"taxicontactlist.plist"];
    NSMutableArray *newList=[[NSMutableArray alloc]init];
    taxiContactRoot=[[NSDictionary alloc]initWithContentsOfFile:path];
    taxiContactNewArray=taxiContactRoot[@"taxi"];
    taxiContact=[taxiContactNewArray valueForKey:@"contactNumber"] ;
    NSArray *s=[[NSArray alloc]init];
    s=[taxiContact valueForKey:@"Number"];
    //taxiContact=[taxiContact valueForKey:@"Number"];
    NSString *str;
    for(int i=0;i<[taxiContact count];i++){
        str=[s objectAtIndex:i];
        [newList addObject:str];
        str=@"";
    }
    NSLog(@"%@",newList);
   [newList writeToFile:taxiPath atomically:YES];

但结果是

enter image description here

1 个答案:

答案 0 :(得分:0)

将JSON读入NSDictionaryNSJSONSerialization)。然后获取taxi NSArray。然后获取联系人:

NSArray *contactNumbers = [taxiArray valueForKey:@"contactNumber"];

保存此数组以生成plist(在磁盘上)。


根据您要提取联系人的方式(及其子信息),您可能需要查看使用关键路径和collection operators(例如@unionOfArrays)。