Convert Json Array to normal Java list


Question

Is there a way to convert JSON Array to normal Java Array for android ListView data binding?

1
125
7/23/2019 9:42:48 PM

Accepted Answer

ArrayList<String> list = new ArrayList<String>();     
JSONArray jsonArray = (JSONArray)jsonObject; 
if (jsonArray != null) { 
   int len = jsonArray.length();
   for (int i=0;i<len;i++){ 
    list.add(jsonArray.get(i).toString());
   } 
} 
178
5/14/2012 4:43:34 AM

If you don't already have a JSONArray object, call

JSONArray jsonArray = new JSONArray(jsonArrayString);

Then 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.

List<String> list = new ArrayList<String>();
for (int i=0; i<jsonArray.length(); i++) {
    list.add( jsonArray.getString(i) );
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon