【问题标题】:Problem with mapping JSON to ArrayList with Jackson - "out of START_OBJECT token"使用 Jackson 将 JSON 映射到 ArrayList 的问题 - “超出 START_OBJECT 令牌”
【发布时间】:2019-09-29 08:26:28
【问题描述】:

我正在尝试将 JSON 映射到 ArrayList,但出现“无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例”错误,我不明白为什么。我找到了这篇文章:Can not deserialize instance of java.util.ArrayList out of START_OBJECT token解决了类似的问题(JSON 无效),但似乎我有有效的 json 文件。我正在使用这个问题中提出的映射方法:How to use Jackson to deserialise an array of objects

我发现这篇文章:Can not deserialize instance of java.util.ArrayList out of START_OBJECT token 解决了类似的问题(JSON 无效),但似乎我有有效的 json 文件。我正在使用这个问题中提出的映射方法:How to use Jackson to deserialise an array of objects

我的 Json 看起来像这样:

[
    { 
        "id": "12345", 
        "name": "John" 
    }, 
    { 
        "id": "09876", 
        "name": "Desmond" 
    }
]

数据模型:

public class Student {

    private int id;
    private String name;

    public Student() {}

    // getters, setters and tostring
}

解析代码:

ObjectMapper objectMapper = new ObjectMapper();

Path pathToStudentsJson = Paths.get("src/main/resources/static/students.json");
File studentsFile = new File(pathToSingleStudentJson.toString());

List<Student> listOfStudents = objectMapper.readValue(studentsFile, new TypeReference<List<Student>>(){});

我得到的错误:

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
 at [Source: (File); line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1139)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1093)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:332)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:265)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2940)
    at io.json.JsonApp.main(JsonApp.java:46)

Process finished with exit code 1

【问题讨论】:

  • 使用版本2.9.8 可以正常工作。你用的是哪个版本?
  • 我也有2.9.8版本!还是一样的错误!

标签: java json arraylist jackson mapping


【解决方案1】:

您的数据模型显示 Id 是 int 而在 JSON 中它是字符串。尝试更改此设置并使用最新版本的 Jackson。它可能会解决问题。

【讨论】:

  • 情况并非如此,因为我正在尝试使用单个学生 JSON 文件,并且 id 已正确映射到 Student 对象内的 int 。无论如何,我可以解决这个问题以保持数据类型一致,但这不是我的问题的解决方案。
【解决方案2】:

好吧,我发现我的错误了 :) 就在这里:

Path pathToStudentsJson = Paths.get("src/main/resources/static/students.json"); 
File studentsFile = new File(pathToSingleStudentJson.toString());

我正在创建引用不正确路径的 File 对象 - 我应该将 pathToStudentsJson 传递给构造函数,而不是 pathToSingleStudentJson

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    相关资源
    最近更新 更多