I've got a little problem with parsing json into my android app.
This is how my json file looks like:
{
"internalName": "jerry91",
"dataVersion": 0,
"name": "Domin91",
"profileIconId": 578,
"revisionId": 0,
}
As You can see this structure is a little bit weird. I dont know how to read that data in my app. As I noticed those are all Objects not arrays :/
UPDATE 2018
After 5 years there is a new "standard" for parsing json on android. It's called moshi and one can consider it GSON 2.0. It's very similar but with design bugs fixed that are the first obstacles when you start using it.
First add it as a mvn dependency like this:
After adding it we can use like so (taken from the examples):
More infos on their GitHub page :)
[old]
I would recommend using Gson.
Here are some links for tutorials:
An alternative to Gson you could use Jackson.
This libraries basically parse your JSON to a Java class you specified.
I suggest you to use a library like gson as @jmeier wrote on his answer. But if you want to handle json with android's defaults, you can use something like this:
Please checkout ig-json parser or Logan Square for fast and light JSON library.
For comparison, this is the stats from Logan Square developer.
to know if string is
JSONArray
orJSONObject
JSONArray
String is like thisand this String as a
JSONOject
but how to call elements from
JSONArray
andJSONObject
?JSNOObject
info called like thisfirst fill object with data
now lets call information from object
If you want to call information from
JSONArray
you must know what is the object position number or you have to loopJSONArray
to get the information for examplelooping array
if i know the object position inside
JSONArray
ill call it like thisYou can always use good old json.org lib. In your Java code :
String
;Then parse it into
JSONObject
:Here you can parse any file from assets folder fetch file from assets folder
Convert JSON to your class object