I want to parse a large JSON. How can I make it faster? Now it takes a lot of time. Please help me.
you can use some wrappers like http://code.google.com/p/json-simple/ it is optimised to handle large data from json and easy to use.
Also you can give it try to http://code.google.com/p/json-simple/ for better performance.
Following are the others.
Android built-in: http://developer.android.com/reference/org/json/package-summary.html
JSON.simple: http://code.google.com/p/json-simple/
Jackson: http://jackson.codehaus.org/
Gson: http://code.google.com/p/google-gson/
You can check comparison article over here.
According to some benchmarks Jackson is the fastest.
Use jackson JSON processor http://jackson.codehaus.org/
Most of the time people end up parsing the whole JSON and ignoring most of the data. This makes the process slow. If you are going to discard a lot of data, parsing in SAX mode is much faster, since you can stop parsing as soon as you have found all the required data.
Related
I would like to know which is more efficient to get the data from the server by the xml or json.
Another question:
does XmlPullParser related to parsing xml data that come from the web service? so if I am using json I don't need XmlPullParser ! or there is other uses !
thank you very much
What I've found extremely useful for parsing JSON is Google's gson library. For xml, you can use gson underneath to do the same thing with gson-xml. With a single line of code you can map your JSON/XML to your objects without having to write a single line of parsing code.
If you find performance to be an issue (I'm making this suggestion because these libs make you super productive), there are mechanisms in both to allow you finer grained control. I doubt you'll have problems with performance though.
For a very thoroughly researched answer to the headline question (though focussed on browsers, not android apps), see David Lee's Balisage 2013 paper:
http://www.balisage.net/Proceedings/vol10/html/Lee01/BalisageVol10-Lee01.html
His conclusion, in one line, is that the choice between XML and JSON makes very little difference in itself - though the details of how you do XML or how you do JSON can make a big difference.
I have an application where I use Jsoup to get HTML file from the internet and parse it into POJOs. I use a custom Content Provider then to persist my POJOs into an SQLite database. It's a lot of code, and certain things are tricky to implement, caching especially (i.e. how to determine that my object is already in database, how to manage expiration, etc.). From looking over the internet I understood that RoboSpice might come to the rescue, since in handles caching transparently. However, I haven't found any example on how to plug in custom parser (my results are neither JSON nor XML, just pure HTML which I'm parsing with Jsoup currently). I'd therefore appreciate if you could point me to some related example.
Here's a more detailed description of what I'm doing. My app reads certain website to get the lists of certain entries. Those entries are calendar-based, and I'm requesting them month by month. Every month's request returns me a list of entries from that month. I want to make those requests cacheable and queryable, therefore I need a database backend, so that I can run custom SQL queries against it. Which RoboSpice configuration should I use, which extensions, and which code samples could I refer to?
Thanks in advance.
It looks like a good idea to use RoboSpice here, but the way you want to use is a bit out of its natural scope.
Usually people annotation a Pojo, let's say for Jackson, and they request a webservice, then the result is parsed via jackson and you get your Pojo. RoboSpice will simply reformat your pojo into json using jackson as parsing / formatting is a considered a bijection.
In your case, you will have to call your own ObjectPersister for your Pojo class and take care of its persistence format yourself. As you store your pojos into a database, the RoboSpice ormlite module may help but it is still experimental.
Have a look at the sample of the ormlite module of RoboSpice.
I am going to be consuming real time twitter feed and parsing it into objects for list view display. I need robust solution is default json lib good enough for this task or do I need to use Gson / jackson lib?
Check Gson and JackSon:
GSON
JACKSON
EDIT: I did not see you mention this libraries. Yes, you should use them. This are robust
libraries, I used them to consume Json webservices and no problem so far.
GSON/Jackson are just the libraries that allow you to parse json responses into objects or the other way round. However, the default implementation of json provided in android framework is capable enough of dealing any response string. The only drawback or turn-off is that you need to write extra code for simple things to get done.
Following are some links with bench marking of JSON libs...
http://blog.novoj.net/2012/02/05/json-java-parsers-generators-microbenchmark/
http://danielywoo.blogspot.in/2011/04/json-java-libraries-benchmark-jsonlib.html
Jackson Vs. Gson
And from the benchmark it looks like Jackson is the best and the fastest library for the JSON parsing...
I have a large json file (~3.5 MB), with (~140) complex objects, and the max depth in the object graph is about 4-5. I use Gson to parse it, but it's really slow. I've tried some way to parse it (like mixed parsing or using stream to parse), but I couldn't increase performance.
I checked Memory Analizer, it kill memory (70-80%), if I only parse the base Id of the objects. While parsing there are 400-500k object in memory (mostly string and char).
Would be parsing more efficient if the object graph wouldn't be so deep? Do you have a good idea how could be better? I tried other libs too (like Jackson), but performance wasn't better.
I had some good performance boost by using Jackson - you should not load whole json into memory but use createJsonParser(Reader r). If it does not work for you then either try spliting data and use separate threads - it makes sense if CPU is not fully loaded by single threaded version. Otherwise try using JNI json parsing implementation.
I am trying to upload & retrieve data with the server.
Which is the best way to retrieve data using xml or json ?
Thank you
As i have used both XML and JSON, and also used all parser including SAX, DOM, Pull Parser. I have also developed web-service for JSON and XML both.
So I suggest you to go with JSON. why?
Because webservice for JSON response seems to develop easily, we don't need to do anything for creating JSON response, we just have to do json_encode() in PHP.
And while in Android, we can parse the JSON string easily by writing less code.
since it supports both.It depends on your feasibility.better go with json as it is easy to implement
I've just done an app that exchanges data over the wire with an API using Google's protocol buffers. There's a neat Java library available from Google for generating the java objects based on your proto files and other libraries to use for parsing an input stream into objects.
Really fast, very low bandwidth, though a bit of an overhead to setup and there's no readable data that you can drop into Notepad to view if you're having trouble.
IMHO the point of choosing one of them goes mainly down to the data size that needs to be transferred to the client. Obviously that should be as small as possible and so the preferred choice is usually
JSON
Google Protocol Buffers
..because they are much more concise than XML.
For data-oriented applications, I prefer JSON to XML due to its simplicity and ease of processing on the client side. XML may be great on the server side, but JSON is definitely easier to deal with on the client side.
have a look on the following url
http://www.subbu.org/blog/2006/08/json-vs-xml
Simplicity
XML is simpler than SGML, but JSON is much simpler than XML. JSON has a much smaller grammar and maps more directly onto the data structures used in modern programming languages.
Extensibility
JSON is not extensible because it does not need to be. JSON is not a document markup language, so it is not necessary to define new tags or attributes to represent data in it.
Interoperability
JSON has the same interoperability potential as XML.
Openness
JSON is at least as open as XML, perhaps more so because it is not in the center of corporate/political standardization struggles.
XML is human readable
JSON is much easier for human to read than XML. It is easier to write, too. It is also easier for machines to read and write.
XML vs JSON
JSON is better then XML that's it.