开发者

Parsing large quantities of JSON data in iOS

I'm getting a large JSON string (11MB) from a web service. When I parse the data using JSONKit, my app reaches 70MB, I get memory warnings, and the app 开发者_Go百科crashes.

How can I parse this data?


the easiest solution is reducing the json size you are getting from the server. If you cant to it, the only way to parse huge JSON is using lazy evaluation.

I dont think there is a JSON lib for objective-c that supports lazy evaluation. however you can implement one.


Your best bet is to look at the YAJL JSON parser, that supports event driven parsing. Then you can parse the JSON as it comes down in a data feed, and not have to store the whole thing in memory at once.

https://github.com/gabriel/yajl-objc

Sorry, I don't know of any code examples that demonstrate this use in practice.


SBJson supports parsing a stream of data. This lets your process your document bit by bit so you don't need to hold on to the entire document. The distribution contains two examples of how to use this. First there's the StreamParserIntegrationTest.m and next there's the TweetStream demo app: a twitter application that will sit and parse a HTTP stream all day (if you let it) displaying each tweet as they come in and then throw them away.

(Disclaimer: I am SBJson's author.)


11 mb is a largedata and solution is only to minimise your size of data JSON support is not native to iOS, but there is a great framework for this exact purpose: http://code.google.com/p/json-framework/

This framework supports conversion from raw JSON objects to Objective-C objects (NSArray, NSDictionary) and vice versa.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜