1

I want to add value to a response. But the response is in this format

   {
  "participants": [
                    {
                      "lat_long": "0.0,0.0", 
                      "name": "alma" 
                     }
                  ],
  "lat_long": "0.0,0.0",
  "_id": "52a80a5dccb8137326000027"
  }

How can I add values to the keys name & lat_long. I am using Sbjson method.

Thanks in advance.

Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29
Giya
  • 250
  • 3
  • 11

2 Answers2

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);

Try this code.

Jitendra
  • 5,055
  • 2
  • 22
  • 42
0

I'm not sure, what you are asking about, but This helps you to get the keys of the 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); 
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • @KumarKl : Thanks... But by using this code i got the key. I want to add value to the key by using PUT method. – Giya Dec 11 '13 at 09:39