derFalke
derFalke

Reputation: 247

parsing json - reached max depth

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?

Upvotes: 1

Views: 1460

Answers (4)

almas
almas

Reputation: 7187

I had the same issue with sbjson. Changing the maxDepth (SBJsonParser.m)to 128 solved the problem.

Upvotes: 0

Stig Brautaset
Stig Brautaset

Reputation: 2632

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.

Upvotes: 0

Matt Long
Matt Long

Reputation: 24486

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.

Upvotes: 2

user529758
user529758

Reputation:

Try my JSON parser library, it has no such limitation:

http://github.com/H2CO3/CarbonateJSON

Upvotes: 0

Related Questions