Put value to a json response by using SBJson

151 views Asked by At

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.

2

There are 2 answers

0
Jitendra On
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.

1
Kumar KL On

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