Json parsing strategy in Android - android

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.

Related

Write/Read large JSONObject to/from file Android

I'm trying to write/read a json object to/from a file.
The json structure is complex and is dynamically generated.
For small json object I would just transform json to string then do string writing/reading. This causes out of memory issue when the json gets too large.
How do I stream the JSONObject directly to a file, and stream back the JSONObject directly from the file?
Having a quick look at the Android api, it seems that you want functionality similar to JsonStringer (https://developer.android.com/reference/org/json/JSONStringer.html) - but it doesn't seem like it is capable of streaming the results. Other than that, I think you'll be bound to either finding a suitable library (possibily https://github.com/yonik/noggit) or implementing your own json streamer (which I doubt would be that hard).
I suggest you use GSON, Google's library to handle JSONs. I use it a lot for converting JSON web responses into classes.
https://github.com/google/gson/blob/master/UserGuide.md

what is the purpose of using JSON in android?

I need to know the purpose of using JSON in android ?
Please anyone tell me in a simple way...
Thanks
The same reason you'd use it on any platform. JSON is a way of storing and expressing information. It uses attribute-value pairs in a hierarchical structure. In Android specifically, you may need to download some information from a database, which could be stored in JSON and then read by your app. Alternatively, you could store data locally in JSON but there are probably better and more efficient ways to do that if you're not sending data across a network.
https://en.wikipedia.org/wiki/JSON
JSON is very light weight, structured, easy to parse and much human readable. JSON is best alternative to XML when your android app needs to interchange data with your server
For example, you can get data Json if you work with database. Or if you work with some API's then you can get data in format Json.
For example an app could fetch data from a server. When using JSON to get the data, the traffic is quite small and the app can easily work with it.
For example you have a server with a database with recipes, and your app displays recipes, the app could ask the server for recipes, and it gets a JSON in return. for example:
{
name: 'Cookies'
ingredients: { 'Butter', 'Eggs', ... /* I don't know, I'm not a chef :D */
...
}
The app can then just read the properties and display them in a neat list ;)
JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is also a subset of JavaScript's Object Notation (the way objects are built in JavaScript
Pls go through this link: http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/
JSON stands for JavaScript Object Notation
JSON is lightweight text-data interchange format
JSON is language independent *
JSON is "self-describing" and easy to understand
* JSON uses JavaScript syntax for describing data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries exists for many different programming languages.
Using JSON in Android is not different than using it on any other platform. The main advantage of the format (in comparison to XML for example) is the small size of the data. This is very important for mobile devices due to the scarce resource those application use - i.e. your mobile app should be able to run with little memory usage, slow internet connection and so on.
Besides Android's framework has built-in tools for parsing / creating JSON objects. Thus it is both easy and efficient to use JSON rather than XML. If you have any project specific reason to prefer another data presentation format - don't worry. It is perfectly fine NOT to use JSON as long as some other format is more suitable for your project.
In short JSON is usually the right choice due to its small footprint and easy of use.

Is it more efficient to use a JSONArray or normal Array for storing/reading data?

I'm working with an app that connects to a PHP/MySQL server from which everything is returned in JSON format. For example, a list of users is returned as a JSONArray of JSONObject. Each object contains the individual user's information (name, location, phone number, etc).
In working with information in this format is is more efficient to leave everything in JSON format and only extract items from the array/objects as needed? Or is it better to extract everything from the JSONArrayand included objects to build a regular Array or ArrayList first?
JSONArray internally uses ArrayList. It's just wrapper over ArrayList. So I'd say there is no difference. JSONObject uses HashMap so again no real drawbacks.
In summary, JSON (which can be thought of a subset of JavaScript) is a lot leaner than XML. This has several positive side-effects
JSON is smaller than corresponding XML
JSON is faster, i.e. simpler syntax -> easier parsing (faster parsing)
JSON was that of JavaScript, I considered it to be a close relative. But JSON is something independent and JSON.org does a great job of describing JSON. It's also provides a compatibility library for JavaScript that adds support for JSON.parse and JSON.stringify when not supported by browsers.
While eval at the time (mid 2009) was used to evaluate JavaScript, it could also evaluate JSON, i.e. parse JSON, but it was considered unsafe, as it did allow arbitrary JavaScript to execute in its stead.
JSON just happens to be a very good fit for browsers and a natural way to evolve the platform due to its close relationship with JavaScript.
While XML might be considered to have better rigor due to the fact that you can type it, it is also those things that make it a lot slower (it is also a bit verbose in my opinion). But if this is something you really want, you should use it, XML is equally ubiquitous.
I will not go into a debate over dynamic or statically typed, but I will say this. It's really easy to add stuff on top of schema-free data and there are plenty of ways to do validation, regardless of schema or no schema.

How to make parsing faster for large and complex JSON?

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.

Speed up JSON objects

I'm using the org.json.* library for turning my web services' result (obviously json) into json objects. My problem is that the JSONObject and JSONArray constructors take a long time to build out the objects. I'm not passing a very large amount of data (anywhere between 1 and 100 array items with 3-5 keys each), but even with just 4 or 5 it takes a few seconds for the constructor to finish.
Is there a way to speed this up? Is there a faster library I could be using?
There's not a whole lot of code to show.
JSONArray arrayjson = new JSONArray(json);
Where json is a String.
You might give gson a try. This article seems to indicate that it has pretty good performance on Android vs the other alternatives. Jackson might be another good alternative.
According to the performance results at https://github.com/eishay/jvm-serializers/wiki, for serialization with databind with strings, e.g., gson.toJson(myObject), Gson is over 10X slower than Jackson. FastJSON beat Jackson at this same test by 2-3%.
Deserialization performance is similar, with Gson over 9x slower than Jackson, and FastJSON about 0.5% faster than Jackson.
Note: The current test results used Gson 1.6. With Gson 1.7.1, the databind performance improved 10-15% over Gson 1.6 (but the manual and manual/tree solutions showed no improvement). The results will hopefully be updated accordingly soon.

Categories

Resources