How can I parse a JSON object from a weblink into Android and store the different values into ArrayLists?
The JSON object of users looks like the below. It comes from a website.
{"Users":[{"name":"Kane","lon":"4.371645","lat":"31.396911"},
{"name":"Sam","lon":"4.129737","lat":"31.194824"},
{"name":"Ryan","lon":"4.023134","lat":"31.298222"},
{"name":"Jerry","lon":"4.262276","lat":"31.295054"}]}
I want to parse in into Android and store the different values like name, lon and lat into an array list.
My ultimate goal is to use the JSON object to place markers on a map activity.
Use GSON library to parse
JSON
to java class. It is very simple to use.Generated model example:
You can use the native JSONObject and JSONArray present on the Android SDK here : https://developer.android.com/reference/org/json/JSONObject.html
There is a method named
JSONObject(String json)
that allow you to do something like this (not tested) :You can create a Model class from your data model like:
After that take a look at GSON library. Using that you can simply import the data as:
After this, just directly retrieve the list from DataTemplate.
Assuming you have a class that can store the results and appropiate constructor you can use code like below, using this json.org library for Java,