I am parsing JSON in my code. But I get some unexpected problems while retrieving the parsed JSON data. So let me explain my problem.
I need to JSON following JSON data using xcode. This is what parses the data when I delete the same URL in the browser:
{ "RESPONSE":[ {"id":"20", "username":"john", "email":" abc@gmail.com ", "phone":"1234567890", "location":"31.000,71.000"}], "STATUS":"OK", "MESSAGE":"Here will be message" }
My code to achieve this JSON data is as follows:
NSData *data = [NSData dataWithContentsOfURL:finalurl]; NSError *error; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
If I print a JSON object using NSLog , i.e. NSLog(@"json: %@", [json description]); , it looks like this:
json: { MESSAGE = "Here will be message"; RESPONSE =( { email = " abc@gmail.com "; id = 20; location = "31.000,71.000"; phone = 1234567890; username = john; } ); STATUS = OK; }
So, here I noticed two things:
The sequence of keys (or nodes) ( MESSAGE , RESPONSE and STATUS ) changes compared to the web response above.
RESPONSE enclosed in brackets '(' & ')' .
Now, if I highlighted the values ββfor the keys MESSAGE and STATUS and NSLog them, then they will be printed as the correct line.
Like msgStr:Here will be message and Status:OK
But , if I highlight the value for RESPONSE as a dictionary and again separating the sub-settings of this dictionary, such as email and username , then I can not get these as a string .
Here is the code I wrote:
NSMutableDictionary *response = [json valueForKey:@"RESPONSE"]; NSString *username = [response valueForKey:@"username"]; NSString *emailId = [response valueForKey:@"email"];
If I print username and emailId , then they do not print as a normal string , instead they are displayed as:
username:( john ) email:( abc@gmail.com )
So my question is why it doesn't work like a normal string? If I tried to use these variables further, they displayed the value enclosed in '(' & ')' curly braces. Is this due to NSJSONSerialization ?