0

I am trying to parse my nested JSON with JSONKit and the 2nd level JSON isn't being parsed correctly.

Here's sample JSON...

{
    "app": {
        "content": "[{\\\"Id\\\":\\\"1\\\",\\\"Name\\\":\\\"John\\\"},{\\\"Id\\\":\\\"2\\\",\\\"Name\\\":\\\"John\\\"}]"
    }
}

and Here's my code...

NSString *jsonString = "...long nested json string...";

NSDictionary *jsonParsed = [jsonString objectFromJSONString];

NSString *content = [[jsonParsed objectForKey:@"app"] objectForKey:@"content"];

NSDictionary *jsonContent = [content objectFromJSONString];

NSLog(@"%@", jsonContent);

Where am I going wrong?

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
Umair A.
  • 6,690
  • 20
  • 83
  • 130

2 Answers2

0

This is quite easy to answer: You are escaping \ as well as ". So your result in the NSString* content will be \". This is something your JSON parser won't digest. So use instead of \\\" this \".

Paul
  • 1,295
  • 2
  • 11
  • 26
  • so is there a way I can tell JSONKit parser to respect these slashes? – Umair A. Jan 01 '12 at 15:51
  • What I said was wrong. It's actually valid JSON starting with an array. This is why JSONKit parses this correctly (I just tested it successfully). – Paul Jan 01 '12 at 16:26
0

If you replace the content string with following:

"[{\"Id\":\"1\",\"Name\":\"John\"},{\"Id\":\"2\",\"Name\":\"John\"}]"

It will be parsed correctly.

JSON.parse("[{\"Id\":\"1\",\"Name\":\"John\"},{\"Id\":\"2\",\"Name\":\"John\"}]")
>>> [Object { Id="1", Name="John"}, Object { Id="2", Name="John"}]

Maybe you have escaped the content string twice at somewhere in your code.


I just used firebug to see if the JSON was correct. JSONKit is the same:

clowwindy:~ clowwindy$ cat /tmp/input.txt 
{
    "app": {
        "content": "[{\"Id\":\"1\",\"Name\":\"John\"},{\"Id\":\"2\",\"Name\":\"John\"}]"
    }
}

NSError *error;
NSString *input = [NSString stringWithContentsOfFile:@"/tmp/input.txt" encoding:NSUTF8StringEncoding error:&error];

NSString *jsonString = input;

NSDictionary *jsonParsed = [jsonString objectFromJSONString];

NSString *content = [[jsonParsed objectForKey:@"app"] objectForKey:@"content"];

NSDictionary *jsonContent = [content objectFromJSONString];

NSLog(@"%@", jsonContent);
NSLog(@"%@", content);

2012-01-02 00:26:39.818 testjson[12700:707] (
        {
        Id = 1;
        Name = John;
    },
        {
        Id = 2;
        Name = John;
    }
)
2012-01-02 00:26:39.822 testjson[12700:707] [{"Id":"1","Name":"John"},{"Id":"2","Name":"John"}]
clowwindy
  • 1,020
  • 8
  • 12
  • That's what I know. But as the string contains triple slashes \\\, what is my option? – Umair A. Jan 01 '12 at 17:31
  • So where does that JSON string come from? Is it generated by some API provided by others, or yourself? If it is generated by yourself, the best way is to generate it correctly. – clowwindy Jan 01 '12 at 18:22
  • Well it's generated by my API. I am generating it using PHP the correct way. JavaScript works fine with this JSON. – Umair A. Jan 02 '12 at 07:56