How to convert JSON string into List of Java object?

前端 未结 9 2092
醉酒成梦
醉酒成梦 2020-12-01 12:12

This is my JSON Array :-

[ 
    {
        \"firstName\" : \"abc\",
        \"lastName\" : \"xyz\"
    }, 
    {
        \"firstName\" : \"pqr\",
        \"         


        
相关标签:
9条回答
  • 2020-12-01 12:22

    For any one who looks for answer yet:

    1.Add jackson-databind library to your build tools like Gradle or Maven

    2.in your Codes:

     add:
                   ObjectMapper mapper = new ObjectMapper();
                   List<Student> studentList = new ArrayList<>();
    
                   studentList = Arrays.asList(mapper.readValue(jsonStringArray, Student[].class));
    
    0 讨论(0)
  • 2020-12-01 12:24

    You are asking Jackson to parse a StudentList. Tell it to parse a List (of students) instead. Since List is generic you will typically use a TypeReference

    List<Student> participantJsonList = mapper.readValue(jsonString, new TypeReference<List<Student>>(){});
    
    0 讨论(0)
  • 2020-12-01 12:26

    You can also use Gson for this scenario.

    Gson gson = new Gson();
    NameList nameList = gson.fromJson(data, NameList.class);
    
    List<Name> list = nameList.getList();
    

    Your NameList class could look like:

    class NameList{
     List<Name> list;
     //getter and setter
    }
    
    0 讨论(0)
  • 2020-12-01 12:27

    Try this. It works with me. Hope you too!

    List<YOUR_OBJECT> testList = new ArrayList<>();
    testList.add(test1);
    
    Gson gson = new Gson();
    
    String json = gson.toJson(testList);
    
    Type type = new TypeToken<ArrayList<YOUR_OBJECT>>(){}.getType();
    
    ArrayList<YOUR_OBJECT> array = gson.fromJson(json, type);

    0 讨论(0)
  • 2020-12-01 12:35

    I made a method to do this below called jsonArrayToObjectList. Its a handy static class that will take a filename and the file contains an array in JSON form.

     List<Items> items = jsonArrayToObjectList(
                "domain/ItemsArray.json",  Item.class);
    
        public static <T> List<T> jsonArrayToObjectList(String jsonFileName, Class<T> tClass) throws IOException {
            ObjectMapper mapper = new ObjectMapper();
            final File file = ResourceUtils.getFile("classpath:" + jsonFileName);
            CollectionType listType = mapper.getTypeFactory()
                .constructCollectionType(ArrayList.class, tClass);
            List<T> ts = mapper.readValue(file, listType);
            return ts;
        }
    
    0 讨论(0)
  • 2020-12-01 12:40

    I have resolved this one by creating the POJO class (Student.class) of the JSON and Main Class is used for read the values from the JSON in the problem.

       **Main Class**
    
        public static void main(String[] args) throws JsonParseException, 
           JsonMappingException, IOException {
    
        String jsonStr = "[ \r\n" + "    {\r\n" + "        \"firstName\" : \"abc\",\r\n"
                + "        \"lastName\" : \"xyz\"\r\n" + "    }, \r\n" + "    {\r\n"
                + "        \"firstName\" : \"pqr\",\r\n" + "        \"lastName\" : \"str\"\r\n" + "    } \r\n" + "]";
    
        ObjectMapper mapper = new ObjectMapper();
    
        List<Student> details = mapper.readValue(jsonStr, new 
          TypeReference<List<Student>>() {      });
    
        for (Student itr : details) {
    
            System.out.println("Value for getFirstName is: " + 
                      itr.getFirstName());
            System.out.println("Value for getLastName  is: " + 
                     itr.getLastName());
        }
    }
    
    **RESULT:**
             Value for getFirstName is: abc
             Value for getLastName  is: xyz
             Value for getFirstName is: pqr
             Value for getLastName  is: str
    
    
     **Student.class:**
    
    public class Student {
    private String lastName;
    
    private String firstName;
    
    public String getLastName() {
        return lastName;
    }
    
    public String getFirstName() {
        return firstName;
    } }
    
    0 讨论(0)
提交回复
热议问题