我正在尝试将一些其他数据插入我的propertylist
数组数据中。
我的实际情况如下:
我从服务器获取了一些JSON
数据并直接加载到plist
。在这个plist中我必须添加一些自定义键和值类型。
我需要在主题数组项中添加isChild
和level
值。请查看下面的图片我有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:
我的代码:
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 :
需要在所有项目中的主题下添加isChild
和level
kyes和值,例如上面的图片!
答案 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"];
}
}