-8

How to get detail data from below JSON data ? I used Objective-C.

{
  "success" : "Y"
  "data": [
    {
      "insertId": "1",
    }
  ]
  "err" : ""
}
aaa
  • 857
  • 4
  • 25
  • 46

2 Answers2

0

what exact data you required.

If you get response then get that response in

  NSDictionary *data = [NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error];

For data:

   NSArray *array=[dictionary objectforKey:@"data"];

You get array [ { "insertId": "1", } ]

    NSdictionary *dict=[array objectAtIndex:0];

you get dictionary in array { "insertId": "1", }

narasimha
  • 57
  • 13
0

I will give you detail solution.

First when you get response check,whether it starts with array or dictionary.

id jsonRes = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

I checked your response.It starts with dictionary.If you have also some doubts, check with below condition.After that get the results from the response using key.

if([jsonRes isKindOfClass:[NSDictionary class]]){
        NSDictionary *dictRes = [jsonRes copy];
        NSString *strSuccess = [dictRes objectForKey:@"success"];
        NSString *strError = [dictRes objectForKey:@"err"];
        NSString *strInsertedId = [[[dictRes objectForKey:@"data"]objectAtIndex:0] objectForKey:@"insertId"];

        NSLog(@"The Success is - %@",strSuccess);
        NSLog(@"Error is - %@",strError);
        NSLog(@"Inserted id is - %@",strInsertedId);
    }
else {
        NSLog(@"The response starts with array");
}

Below I tried your code manually,it works fine.I created json using string and I checked the result.

NSString *strJson = @"{ \"success\": \"Y\", \"data\": [{\"insertId\": \"1\" }],\"err\": \"\"}" ;
NSError *jsonError;
NSDictionary *dictRes =  [NSJSONSerialization JSONObjectWithData: [strJson dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&jsonError];
NSLog(@"The response are - %@",dictRes);
NSString *strSuccess = [dictRes objectForKey:@"success"];
NSString *strError = [dictRes objectForKey:@"err"];
NSString *strInsertedId = [[[dictRes objectForKey:@"data"]objectAtIndex:0] objectForKey:@"insertId"];

NSLog(@"The Success is - %@",strSuccess);
NSLog(@"Error is - %@",strError);
NSLog(@"Inserted id is - %@",strInsertedId);

Output result is

enter image description here

user3182143
  • 9,459
  • 3
  • 32
  • 39
  • Please don't post answers to such terrible questions. All you are doing is encouraging bad questions on this site. Instead, vote to close the question and put your efforts into questions that show their own effort. – rmaddy Dec 12 '17 at 15:10