jackson deserialization json to java-objects

前端 未结 4 829
时光取名叫无心
时光取名叫无心 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条回答
  •  醉话见心
    2020-12-09 08:48

    It looks like you are trying to read an object from JSON that actually describes an array. Java objects are mapped to JSON objects with curly braces {} but your JSON actually starts with square brackets [] designating an array.

    What you actually have is a List To describe generic types, due to Java's type erasure, you must use a TypeReference. Your deserialization could read: myProduct = objectMapper.readValue(productJson, new TypeReference>() {});

    A couple of other notes: your classes should always be PascalCased. Your main method can just be public static void main(String[] args) throws Exception which saves you all the useless catch blocks.

提交回复
热议问题