json to arraylist code example
Example 1: objectmapper convert list of objects
List myObjects = mapper.readValue(jsonInput, new TypeReference>(){});
Example 2: jsonarray to list java
JSONArray data = new JSONArray(); //create data from this -> [{"thumb_url":"tb-1370913834.jpg","event_id":...}]
List list = data.stream().map(o -> (JSONObject) o).collect(Collectors.toList());
Example 3: arraylist to json array
ArrayList list = new ArrayList();
list.add("foo");
list.add("baar");
JSONArray jsArray = new JSONArray(list);
Example 4: convert json to arraylist java
// Top of file
import java.lang.reflect.Type;
// ...
private void parseJSON() {
Gson gson = new Gson();
Type type = new TypeToken>(){}.getType();
List contactList = gson.fromJson(jsonString, type);
for (ContactModel contact : contactList){
Log.i("Contact Details", contact.id + "-" + contact.name + "-" + contact.email);
}
}