如何使用 Jackson 序列化和反序列化对象列表
How to serialize and deserialize a list of objects using Jackson
反序列化对象数组时出现错误。
ans int 下面的表达式是 List<Restaurant>
类型
String json = obj.writeValueAsString(ans);
我在下面的行中收到错误
List<Restaurant> all= Arrays.asList(obj.readValue(reslistjson,Restaurant[].class));
错误-
无法从 START_OBJECT 令牌中反序列化 com.crio.qeats.dto.Restaurant[]
的实例
在 [来源:(字符串)"{"restaurantId":"12","name":"A2B","city":"Electronic City","imageUrl":"www.google.com","latitude":20.015,"longitude":30.015,"opensAt":"18:00","closesAt":"23:00","attributes" :["Tamil","South Indian"]}";行:1,列:1]
JSON 您示例中的输入是对象而不是数组。
对于您的 JSON 数据,这会起作用:
List<Restaurant> all= Arrays.asList(objectMapper.readValue(json,Restaurant.class));
像这样的 JSON 是一个对象数组,您的原始代码可以工作:
String json = "[{..data1 goes here....}, {..data2 goes here....}]";
List<Restaurant> all= Arrays.asList(objectMapper.readValue(json,Restaurant[].class));
反序列化对象数组时出现错误。
ans int 下面的表达式是 List<Restaurant>
类型
String json = obj.writeValueAsString(ans);
我在下面的行中收到错误
List<Restaurant> all= Arrays.asList(obj.readValue(reslistjson,Restaurant[].class));
错误-
无法从 START_OBJECT 令牌中反序列化 com.crio.qeats.dto.Restaurant[]
的实例
在 [来源:(字符串)"{"restaurantId":"12","name":"A2B","city":"Electronic City","imageUrl":"www.google.com","latitude":20.015,"longitude":30.015,"opensAt":"18:00","closesAt":"23:00","attributes" :["Tamil","South Indian"]}";行:1,列:1]
JSON 您示例中的输入是对象而不是数组。 对于您的 JSON 数据,这会起作用:
List<Restaurant> all= Arrays.asList(objectMapper.readValue(json,Restaurant.class));
像这样的 JSON 是一个对象数组,您的原始代码可以工作:
String json = "[{..data1 goes here....}, {..data2 goes here....}]";
List<Restaurant> all= Arrays.asList(objectMapper.readValue(json,Restaurant[].class));