Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Json Array to normal Java list

Tags:

java

json

android

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

like image 615
Houston we have a problem Avatar asked Aug 03 '10 10:08

Houston we have a problem


People also ask

Can we convert JSON array to list in Java?

We can convert a JSON array to a list using the ObjectMapper class. It has a useful method readValue() which takes a JSON string and converts it to the object class specified in the second argument.

Can we convert JSON array to string?

int size = exampleList. size(); String[] stringArray = exampleList. toArray(new String[size]); This will convert our JSON array into a String array.

Can we convert JSON to array?

Convert JSON to Array Using `json. The parse() function takes the argument of the JSON source and converts it to the JSON format, because most of the time when you fetch the data from the server the format of the response is the string. Make sure that it has a string value coming from a server or the local source.


2 Answers

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());    }  }  
like image 107
Pentium10 Avatar answered Oct 01 '22 03:10

Pentium10


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) ); } 
like image 43
Nick Avatar answered Oct 01 '22 05:10

Nick