OStack程序员社区-中国程序员成长平台

标题: ios - 如何使用 Objective C 将 JSON 解析的数据存储到 Plist 中? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:21
标题: ios - 如何使用 Objective C 将 JSON 解析的数据存储到 Plist 中?

我需要使用 objective CJSON 解析的数据存储到 propertylist 中,如下结构。

我的来自服务器的 JSON 响应:

{ 
   "response":{ 
      "A":{ 
         "name":"Arun",
         "age":"20",
         "city":"SFO",
         "subject":2
      },
      "B":{ 
         "name":"Benny",
         "age":"20",
         "city":"SFO",
         "subject":1
    },
      "C":{ 
         "name":"Nani",
         "age":"30",
         "city":"SFO",
         "subject":0
      }
   },
   "inprogressdata":{ 
   },
   "dataspeed":"112 milliseconds..."
}

我正在尝试将以下存储方法放入 propertylist。进入这个属性列表 Item 0 , 1, 2 它称为 JSON 响应 A, B, C 值 Into Array 和 Item 应该作为字典。

注意: A, B, C 值将根据 JSON 响应增加,我的意思是它不是静态的,它可能会上升到 Z。

enter image description here

在这个Array 值中,每个值首先应该存储为一个dictionary。在该字典中需要添加如下字符串值,如果基于该计数的主题计数高于 0 它应该创建字典数组,如下图所示。

enter image description here

例如:

-Root 
|
|-----Objects [Array Value]
       |
       |------Item 0(A) [Dictionary Value]   
       |
       |------name (String)
       |------age (String)
       |------city (String)
       |------subject (Number)     // If number 2 then need to create "Objects_Subjectcount" [Array Values]
       |------Objects_Subjectcount (Array)
             |
             |------Item 0 [Dictionary Value]
             |------Item 1 [Dictionary Value]    



Best Answer-推荐答案


试试这个代码

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSDictionary *response = JSON[@"response"];
NSArray *objects = [response allValues];
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = paths.firstObject;
NSString *plistPath = [documentsPath stringByAppendingPathComponent"File.plist"];
NSError *writeError = nil;
NSData *plistData = [NSPropertyListSerialization dataWithPropertyListbjects format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
if(plistData){
    [plistData writeToFile:plistPath atomically:YES];
}
else {
    NSLog(@"Error in saveData: %@", error);
}

如果我对您问题第二部分的理解是正确的,代码将是这样的

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
 NSDictionary *response = JSON[@"response"];
 NSArray *keys = [response allKeys];

 NSMutableArray *objects = [NSMutableArray new];
 for (NSString *key in keys) {
     NSMutableDictionary *object = response[key];
     NSPredicate *predicate = [NSPredicate predicateWithFormat"subject = %@",object[@"subject"]];
     NSArray *objectsWithSameSubject = [objects filteredArrayUsingPredicate:predicate];
     NSInteger subjects = [object[@"subject"] integerValue];
     if (subjects > 0) {

         NSMutableArray *Objects_Subjectcount = [NSMutableArray new];
         [object setObject:Objects_Subjectcount forKey"Objects_Subjectcount"];
         for (NSInteger i = 0; i < subjects; i++) {
             [Objects_Subjectcount addObjectbject];// object or anything you need

         }
     }
     [objects addObjectbject];
 }

 NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsPath = paths.firstObject;
 NSString *plistPath = [documentsPath stringByAppendingPathComponent"File.plist"];
 NSError *writeError = nil;
 NSDictionary *finalDict = @{@"Objects": objects};
 NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
 if(plistData){
     [plistData writeToFile:plistPath atomically:YES];
 }
 else {
     NSLog(@"Error in saveData: %@", error);
 }

步骤是,

1. Create JSON with mutable containers and leaves.
2. Iterate through all keys and values.
3. Check whether the subject value of object is greater than 0 in the existing subjects array? if yes add the object to
Objects_Subjectcount. Add the object to objects array.(Don't forget
to set an NSMutable array with key Objects_Subjectcount) to the
object. 4 . Make final dictionary to be written as plist - wrap the
objets array in a dictionary with key "objects"
5. Convert the dictionary to plist data and write it

关于ios - 如何使用 Objective C 将 JSON 解析的数据存储到 Plist 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34488990/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4