• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何使用 Objective C 将 JSON 解析的数据存储到 Plist 中?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 12:21:49 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我需要使用 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap