如何将自定义值插入现有的Plist数组数据?

时间:2015-12-30 16:43:21

标签: ios objective-c json nsmutablearray nsdictionary

我正在尝试将一些其他数据插入我的propertylist数组数据中。

我的实际情况如下:

我从服务器获取了一些JSON数据并直接加载到plist。在这个plist中我必须添加一些自定义键和值类型。 我需要在主题数组项中添加isChildlevel值。请查看下面的图片我有red marked,我想要实现的目标。

我的JSON回复:

{ 
   "response":{ 
      "A":{ 
         "name":"Arun",
         "age":"20",
         "city":"SFO",
         "subject":[{
            "place":SFO
        }]
      },
      "B":{ 
         "name":"Benny",
         "age":"20",
         "city":"SFO",
         "subject":[{
            "place":SFO
            },
            {
            "place":ADS
        }]
    },
      "C":{ 
         "name":"Nani",
         "age":"30",
         "city":"SFO",
      }
   },
   "inprogressdata":{ 
   },
   "dataspeed":"112 m..."
}

我的Plist:

enter image description here

我的代码:

NSError *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];
            [object setObject:[NSNumber numberWithInt:0] forKey:@"level"];
            [object setObject:[NSNumber numberWithBool:YES] forKey:@"isParent"]; // Here I am adding isParent easily
            [objects addObject:object];

            //NSLog(@"SIGNALEVENT :%@",[object valueForKey:@"subject"]);
            //Here I need to add isChild Boolean and level values under the subject within subject Items like above Image
        }

MY Issue :需要在所有项目中的主题下添加isChildlevel kyes和值,例如上面的图片!

1 个答案:

答案 0 :(得分:1)

for (NSString *key in keys) {
        NSMutableDictionary *object = response[key];
        NSMutableArray * subjectArray = object[@"subject"];
        for (NSMutableDictionary * dict in subjectArray) {
            [dict setObject:[NSNumber numberWithBool:true] forKey:@"isChild"];
            [dict setObject:[NSNumber numberWithInt:1] forKey:@"level"];
        }
    }