Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am downloading a JSON string and converting it to JSONArray. Im putting it into a listview and need to be able to delete from that listview later, and since JSONArray has no .remove method (Thanks Obama), I am trying to convert it to an arraylist.
here is my JSON (the array.toString()):
"thumb_url":"tb1370913834.jpg",
"event_id":"15","count":"44",
"event_tagline":"this is a tagline",
"event_name":"5th birthday",
"event_end":"1370919600",
"event_start":"1370876400"
I need to get it into an array and be able to call the strings by their respective keys. Appreciate any help!
–
–
–
ArrayList<String> listdata = new ArrayList<String>();
JSONArray jArray = (JSONArray)jsonObject;
if (jArray != null) {
for (int i=0;i<jArray.length();i++){
listdata.add(jArray.getString(i));
–
–
I've done it using Gson
(by Google).
Add the following line to your module's build.gradle
:
dependencies {
// ...
// Note that `compile` will be deprecated. Use `implementation` instead.
// See https://stackoverflow.com/a/44409111 for more info
implementation 'com.google.code.gson:gson:2.8.2'
JSON
string:
private String jsonString = "[\n" +
" {\n" +
" \"id\": \"c200\",\n" +
" \"name\": \"Ravi Tamada\",\n" +
" \"email\": \"ravi@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c201\",\n" +
" \"name\": \"Johnny Depp\",\n" +
" \"email\": \"johnny_depp@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c202\",\n" +
" \"name\": \"Leonardo Dicaprio\",\n" +
" \"email\": \"leonardo_dicaprio@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c203\",\n" +
" \"name\": \"John Wayne\",\n" +
" \"email\": \"john_wayne@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c204\",\n" +
" \"name\": \"Angelina Jolie\",\n" +
" \"email\": \"angelina_jolie@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c205\",\n" +
" \"name\": \"Dido\",\n" +
" \"email\": \"dido@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c206\",\n" +
" \"name\": \"Adele\",\n" +
" \"email\": \"adele@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c207\",\n" +
" \"name\": \"Hugh Jackman\",\n" +
" \"email\": \"hugh_jackman@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c208\",\n" +
" \"name\": \"Will Smith\",\n" +
" \"email\": \"will_smith@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c209\",\n" +
" \"name\": \"Clint Eastwood\",\n" +
" \"email\": \"clint_eastwood@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c2010\",\n" +
" \"name\": \"Barack Obama\",\n" +
" \"email\": \"barack_obama@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c2011\",\n" +
" \"name\": \"Kate Winslet\",\n" +
" \"email\": \"kate_winslet@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"female\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c2012\",\n" +
" \"name\": \"Eminem\",\n" +
" \"email\": \"eminem@gmail.com\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" }\n" +
" ]";
ContactModel.java
:
public class ContactModel {
public String id;
public String name;
public String email;
Code for converting a JSON string to ArrayList<Model>
:
Note: You have to import java.lang.reflect.Type;
:
// Top of file
import java.lang.reflect.Type;
// ...
private void parseJSON() {
Gson gson = new Gson();
Type type = new TypeToken<List<ContactModel>>(){}.getType();
List<ContactModel> contactList = gson.fromJson(jsonString, type);
for (ContactModel contact : contactList){
Log.i("Contact Details", contact.id + "-" + contact.name + "-" + contact.email);
Hope this will help you.
–
–
–
public static ArrayList<Object> convert(JSONArray jArr)
ArrayList<Object> list = new ArrayList<Object>();
try {
for (int i=0, l=jArr.length(); i<l; i++){
list.add(jArr.get(i));
} catch (JSONException e) {}
return list;
public static JSONArray convert(Collection<Object> list)
return new JSONArray(list);
Usage:
ArrayList<Object> list = ArrayUtil.convert(jArray);
JSONArray jArr = ArrayUtil.convert(list);
–
try this way
Simply loop through that, building your own array. This code assumes it's an array of strings, it shouldn't be hard to modify to suit your particular array structure.
JSONArray jsonArray = new JSONArray(jsonArrayString);
List<String> list = new ArrayList<String>();
for (int i=0; i<jsonArray.length(); i++) {
list.add( jsonArray.getString(i) );
Instead of converting the JSON string to an ArrayList or even a Map, you can just create a JSONObject itself. This object has the ability to get string values by key, as you want and also to remove objects.
To create a JSONObject
from a properly formatted JSON string, you simply call the appropriate constructor.
JSONObject json = new JSONObject(jsonString);
–
–
–
–
List<YourPojoObject> yourPojos = new ArrayList<YourPojoObject>();
JSONObject jsonObject = new JSONObject(jsonString);
YourPojoObject yourPojo = new YourPojoObject();
yourPojo.setId(jsonObject.getString("idName"));
yourPojos.add(yourPojo);
I have fast solution. Just create a file ArrayUtil.java
ObjectMapper mapper = new ObjectMapper();
List<Student> list = Arrays.asList(mapper.readValue(jsonString, Student[].class));
try {
JSONArray jsonArr = new JSONArray("Your json string array");
List<AllAppModel> lstExtrextData = new ArrayList<>();
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
AllAppModel data = new AllAppModel();
data.setAppName(jsonObj.getString("appName"));
data.setPackageName(jsonObj.getString("packageName"));
data.setUid(jsonObj.getInt("uid"));
data.setSelected(jsonObj.getBoolean("isSelected"));
data.setSystemApp(jsonObj.getBoolean("isSystemApp"));
data.setFav(jsonObj.getBoolean("isFav"));
lstExtrextData.add(data);
} catch (JSONException e) {
e.printStackTrace();
it will return you List of PoJo class object.
Just going by the original subject of the thread:
converting jsonarray to list (used jackson jsonarray and object mapper here):
ObjectMapper mapper = new ObjectMapper();
JSONArray array = new JSONArray();
array.put("IND");
array.put("CHN");
List<String> list = mapper.readValue(array.toString(), List.class);
If the Json object consists an array of strings, then there is a way where we need not even use the JSONArray.
if the Json is not in string format, we can convert the json into a string.
Now if Json string is:
String value = "[\"value1\",\"value2\"]";
Then we can use to get the List of String:
List<String> result = new Gson().fromJson(value, List.class);
List<Student> students = new ArrayList<>();
JSONArray jsonArray = new JSONArray(stringJsonContainArray);
for (int i = 0; i < jsonArray.length(); i++) {
Student student = new Gson().fromJson(jsonArray.get(i).toString(), Student.class);
students.add(student);
return students;
JSONArray data = jsonObject.getJSONArray("some-node");
List<JSONObject> list = StreamSupport.stream(data.spliterator(), false)
.map(e -> (JSONObject)e)
.collect(Collectors.toList());
–
Single line code that I used to convert json array to ArrayList for the below listed json string stored in a variable named classData.
List<String> names = new Gson().fromJson(classData.getAsJsonObject("result").getAsJsonArray("names").toString(), new TypeToken<List<String>>(){}.getType());
"result": {
"id": "104297",
"class": "Chemistry",
"total": 57,
"names": [
"Alex",
"Dan",
"Rex",
"Tony",
"Elizabeth"
public static List<JSONObject> getJSONObjectListFromJSONArray(JSONArray array)
throws JSONException {
ArrayList<JSONObject> jsonObjects = new ArrayList<>();
for (int i = 0;
i < (array != null ? array.length() : 0);
jsonObjects.add(array.getJSONObject(i++))
return jsonObjects;
public static <T> List<T> getList(JSONArray jsonArray) throws Exception {
List<T> list = new ArrayList<>(jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
list.add((T)jsonArray.get(i));
return list;
//Usage
List<String> listKeyString = getList(dataJsonObject.getJSONArray("keyString"));
ArrayList<String> listdata = new ArrayList<String>();
JSONArray jArray = (JSONArray)jsonObject;
if (jArray != null) {
listdata.addAll(jArray);
@simplified
A simpler Java 8 alternative:
JSONArray data = new JSONArray(); //create data from this -> [{"thumb_url":"tb-1370913834.jpg","event_id":...}]
List<JSONObject> list = data.stream().map(o -> (JSONObject) o).collect(Collectors.toList());
–
With Kotlin, you can avoid a loop by wrapping the JSONArray with a MutableList, e.g.
val artistMetadata = player.metadata.optJSONArray("artist")
val artists = MutableList<String>(artistMetadata.length()) { i -> artistMetadata.getString(i)}
public static List<String> convertJsonArrayToStringList(JsonArray ja){
List<String> result = new ArrayList<String>();
for(JsonElement je: ja)
result.add(je.getAsString());
return result;
This can do (Java >= 8
):
// Functional (variable)
Function<JSONArray, ArrayList<String>> convert = jsonArray -> (ArrayList<String>) jsonArray.stream()
.map(item -> item.toString())
.collect(Collectors.toList());
// Call
convert.apply(jsonArray);
// Regular method (OOP)
private Function<JSONArray, ArrayList<String>> convert() {
return jsonArray -> (ArrayList<String>) jsonArray.stream()
.map(item -> item.toString())
.collect(Collectors.toList());
// Call
convert().apply(jsonArray);
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.