This is an example of how to use Android's JsonReader class to read data from a JSON array and convert the JSON array of JSON objects to an ArrayList of Java objects.
The app downloads a JSON script from a server. The JSON-Script contains an array of persons with their name, profession and age. The app decodes this information and saves it as an ArrayList of Java objects of the class "Person". At the end the app displays all the data to the user using a textview (as you can see in the screenshot).
I wrote lots of comments in the code, so it should be easy to understand.
- jsonscript_persons.json (The JSON-file we're reading from)
- MainActivity.java (This is where all the work happens)
- Person.java (A data class to create an object for each person)
- AndroidManifest.xml (Don't forget the permissions in the manifest!)
- activity_main.xml (The layout of the app)
{
"user":[
{
"name":"John Doe",
"profession":"Doctor",
"age":27
},
{
"name":"Mike Miller",
"profession":"Musician",
"age":40
},
{
"name":"Bob Graham",
"profession":"Programmer",
"age":32
}
]
}