1

I m performing JSON parsing. In return i m getting the result something like this:

{"login":"FALSE","userid":"Please enter correct Email Address"}

I want to retain the userid: message into some variable, so that i can perform functions on the basis of userid itself.

Suggestion please.

Thanks

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
FirstTimer
  • 313
  • 1
  • 5
  • 14
  • some 'literature': http://en.wikipedia.org/wiki/JSON, this answer might help you: http://stackoverflow.com/a/5813223/653513 – Rok Jarc Jun 07 '12 at 15:19
  • 1
    If you're o.k. with supporting iOS >= 5.0 then you'll probably be best off using [NSJSONSerialization Class](http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html) – Rok Jarc Jun 07 '12 at 15:39
  • my app will be dealing with iOS >= 5 only....i m quite confused about taking that userid: "message" into some string.not able to get it – FirstTimer Jun 07 '12 at 15:47

1 Answers1

0

You can try with a framework such as this,https://github.com/stig/json-framework

Example usage would be, assuming userId is declared in your header file.

SBJSON *parser = [[SBJSON alloc] init];

NSArray *statuses = [parser objectWithString:json_string error:nil];

userId = [statuses valueForKey:@"userid"]; 

Now userId retains the value for the json value for userid from the json string

UPDATE: If you're only supporting iOS >= 5.0 I would suggest the built in NSJSONSerialization class. If this is the case, your code will look more like this..

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSMutableArray *jsonList = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

userId = [jsonList valueForKey:@"userid"];

NSLog(@"userid: %@", userId); // will print "Please enter correct Email Address"

Hope this helps !

skram
  • 5,314
  • 1
  • 22
  • 26