How to get detail data from below JSON data ? I used Objective-C.
{
"success" : "Y"
"data": [
{
"insertId": "1",
}
]
"err" : ""
}
How to get detail data from below JSON data ? I used Objective-C.
{
"success" : "Y"
"data": [
{
"insertId": "1",
}
]
"err" : ""
}
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", }
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