我想为响应添加值。但响应采用这种格式
{
"participants": [
{
"lat_long": "0.0,0.0",
"name": "alma"
}
],
"lat_long": "0.0,0.0",
"_id": "52a80a5dccb8137326000027"
}
如何为键名添加值& lat_long。我正在使用Sbjson方法。
提前致谢。
答案 0 :(得分:1)
NSURL * url=[NSURL URLWithString:@"Give your URL Here"];
NSData * data=[NSData dataWithContentsOfURL:url];
NSError * error;
NSMutableDictionary * jsonDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"%@",jsonDic);
NSMutableArray * jsonArray=jsonDic[@"participants"];
NSLog(@"%@",jsonArray);
NSString * str =[jsonArray valueForKey:@"lat_long"];
NSString * str1 =[jsonArray valueForKey:@"name"];
NSLog(@"%@",str);
NSLog(@"%@",str1);
试试这段代码。
答案 1 :(得分:0)
我不确定,你在询问什么,但这可以帮助你获得Disctionary的钥匙。
NSDictionary *json = [NSJSONSerialization .....//Parsed JSon
NSMutableDictionary *arrCopy = [json mutableCopy];
NSArray *keys= [json allKeys];
for (NSString *keysV in keys){
NSLog(@"Keys are %@", keysV);
if ([keysV isEqualToString:@"_id"]) {
[arrCopy setValue:@"121213211323" forKey:keysV];
}else if (){
}.......................
}
NSLog(@"After Value added: %@", arrCopy);