Is there a way to convert JSON Array to normal Java Array for android ListView data binding?
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.
int size = exampleList. size(); String[] stringArray = exampleList. toArray(new String[size]); This will convert our JSON array into a String 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.
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()); } }
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) ); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With