parsing json - reached max depth

梦想的初衷 提交于 2019-12-11 09:33:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!