JSON parsing using NSJSONSerialization
NSString* path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
//Here you can take JSON string from your URL ,I am using json file
NSString* jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError;
NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&jsonError];
NSLog(@"jsonDataArray: %@",jsonDataArray);
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError];
if(jsonObject !=nil){
// NSString *errorCode=[NSMutableString stringWithFormat:@"%@",[jsonObject objectForKey:@"response"]];
if(![[jsonObject objectForKey:@"#data"] isEqual:@""]){
NSMutableArray *array=[jsonObject objectForKey:@"#data"];
// NSLog(@"array: %@",array);
NSLog(@"array: %d",array.count);
int k = 0;
for(int z = 0; z<array.count;z++){
NSString *strfd = [NSString stringWithFormat:@"%d",k];
NSDictionary *dicr = jsonObject[@"#data"][strfd];
k=k+1;
// NSLog(@"dicr: %@",dicr);
NSLog(@"Firstname - Lastname : %@ - %@",
[NSMutableString stringWithFormat:@"%@",[dicr objectForKey:@"user_first_name"]],
[NSMutableString stringWithFormat:@"%@",[dicr objectForKey:@"user_last_name"]]);
}
}
}
You can see the Console output as below :
Firstname - Lastname : Chandra Bhusan - Pandey
Firstname - Lastname : Kalaiyarasan - Balu
Firstname - Lastname : (null) - (null)
Firstname - Lastname : Girija - S
Firstname - Lastname : Girija - S
Firstname - Lastname : (null) - (null)