Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 crashes.

How can I parse this data?

like image 924
aViNaSh Avatar asked Sep 02 '11 14:09

aViNaSh


People also ask

What is JSON parsing in iOS?

JSON parsing in Swift is a common thing to do. Almost every app decodes JSON to show data in a visualized way. Parsing JSON is definitely one of the basics you should learn as an iOS developer. Decoding JSON in Swift is quite easy and does not require any external dependencies.

What is the fastest JSON parser?

We released simdjson 0.3: the fastest JSON parser in the world is even better! Last year (2019), we released the simjson library. It is a C++ library available under a liberal license (Apache) that can parse JSON documents very fast.

Is JSON difficult to parse?

JSON is a data interchange format that is easy to parse and generate. JSON is an extension of the syntax used to describe object data in JavaScript.

How big is too big for JSON?

One of the more frequently asked questions about the native JSON data type, is what size can a JSON document be. The short answer is that the maximum size is 1GB.


3 Answers

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.

like image 99
Guy Ephraim Avatar answered Oct 17 '22 23:10

Guy Ephraim


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.

like image 44
Kendall Helmstetter Gelner Avatar answered Oct 17 '22 23:10

Kendall Helmstetter Gelner


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.)

like image 2
Stig Brautaset Avatar answered Oct 17 '22 21:10

Stig Brautaset