简体   繁体   中英

to store list from JSON object to java arraylist

I'm new to JSON. I need to store the list from JSON object to java arraylist. In JSON object I'm having list of object. which is need to store in Arraylist list = new ArrayList<>();

You can check this example, it specifies exactly what you need

https://www.javatpoint.com/how-to-convert-json-array-to-arraylist-in-java

    JSONObject jsnobject = new JSONObject(json);  
    JSONArray jsonArray = jsnobject.getJSONArray("languages");  
    ArrayList<Object> listdata = new ArrayList<Object>();  
    if (jsonArray != null) {   
        for (int i=0;i<jsonArray.length();i++){   
            listdata.add(jsonArray.get(i));  
        }   
    } 

or java 8 style I recommend to use this one

https://mkyong.com/java/jackson-convert-json-array-string-to-list/

ObjectMapper mapper = new ObjectMapper();
List<Person> pp3 = mapper.readValue(json, new TypeReference<List<Person>>() {});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM