jackson deserialization json to java-objects

前端 未结 4 823
时光取名叫无心
时光取名叫无心 2020-12-09 08:18

Here is my Java code which is used for the de-serialization, i am trying to convert json string into java object. In doing so i have used the following code:



        
4条回答
  •  猫巷女王i
    2020-12-09 08:44

    You have to change the line

    product userFromJSON = mapper.readValue(userDataJSON, product.class);
    

    to

    product[] userFromJSON = mapper.readValue(userDataJSON, product[].class);
    

    since you are deserializing an array (btw: you should start your class names with upper case letters as mentioned earlier). Additionally you have to create setter methods for your fields or mark them as public in order to make this work.

    Edit: You can also go with Steven Schlansker's suggestion and use

    List userFromJSON =
            mapper.readValue(userDataJSON, new TypeReference>() {});
    

    instead if you want to avoid arrays.

提交回复
热议问题