反序列化对象数组时出现错误。

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[]的实例
在[来源:(String)“ {” restaurantId“:” 12“,” name“:” A2B“,” city“:”电子城市“,” imageUrl“:” www.google.com“,”纬度“: 20.015,“经度”: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));

关于java - 如何使用Jackson序列化和反序列化对象列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56536639/

10-08 21:22