问题
I want to parse the comments of a reddit post with over 500 comments. For example this one: http://www.reddit.com/comments/xu11o The json url is: http://www.reddit.com/comments/xu11o.json
In am using SBJson to achieve this.
When I try to get a NSArray with this code:
NSString* response = [request responseString];
NSArray* responseArray = [response JSONValue];
I get this error message: -JSONValue failed. Error is: Input depth exceeds max depth of 32
Changing the depth to a higher number of for example 100 makes my app crash.
If the reddit post has only 20 comments I get the NSArray and can successfully display them.
What do I have to change to get the NSArray?
回答1:
Have you tried Apple's NSJSONSerialization JSON parsing library? It works.
NSString *urlString = @"http://www.reddit.com/comments/xu11o.json";
NSURL *url = [NSURL URLWithString:urlString];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:
[NSURLRequest requestWithURL:url]
returningResponse:&response
error:&error];
id jsonObj = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
// Do something with jsonObj which is an array.
Just make sure you switch your download code to asynchronous before shipping.
Best regards.
回答2:
Try my JSON parser library, it has no such limitation:
http://github.com/H2CO3/CarbonateJSON
回答3:
This "limitation" of SBJsonParser is a security feature, protecting you from presumed malicious JSON. The limit is configurable through the maxDepth property. The default is 32, as you've found. You can change it to any integer value you want, or turn the max depth check off by setting it to 0.
回答4:
I had the same issue with sbjson. Changing the maxDepth (SBJsonParser.m)to 128 solved the problem.
来源:https://stackoverflow.com/questions/11854635/parsing-json-reached-max-depth