-2

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
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
satvinder singh
  • 1,152
  • 1
  • 12
  • 22

2 Answers2

3

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!"];

                }
Saad Chaudhry
  • 1,392
  • 23
  • 37
1

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"];
Khawar Ali
  • 3,462
  • 4
  • 27
  • 55