i have just started developing ios apps. I am writing an ios app, and need to parse json response got from server in iphone app
{
"response": "login success",
"response_code": 1
}
i have just started developing ios apps. I am writing an ios app, and need to parse json response got from server in iphone app
{
"response": "login success",
"response_code": 1
}
use NSJSONSerialization
to parse json response
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"json data is %@",jsonData);
NSInteger success = [[jsonData objectForKey:@"response_code"] integerValue];
NSString *response = [jsonData objectForKey:@"response"];
NSLog(@"success is %d",success);
check the response code here
if(success == 1)
{
// navigate to next or do whatever
// [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
}
You should use this framework: https://github.com/stig/json-framework
and SBJsonParser
class to parse the JSon script. The below code will help you parse json response:
SBJSON *parser = [[SBJSON alloc] init] ;
NSDictionary *dic = (NSDictionary *)[parser objectWithString:respString error:nil];
This code will convert your response string in the respString
variable into an NSDictionary, and now you can extract each object by calling:
NSString* response = [dic objectForKey:@"response"];