且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法从START_OBJECT标记中反序列化java.util.ArrayList的实例

更新时间:2022-06-11 17:23:02

问题是JSON - 默认情况下,这不能被反序列化为集合,因为它实际上不是JSON数组 - 看起来像这样:

The problem is the JSON - this cannot, by default, be deserialized into a Collection because it's not actually a JSON Array - that would look like this:

[
    {
        "name": "Test order1",
        "detail": "ahk ks"
    },
    {
        "name": "Test order2",
        "detail": "Fisteku"
    }
]

因为你没有控制反序列化的确切过程(RestEasy会这样做) - 第一个选项会是简单地将JSON注入 String ,然后控制反序列化过程:

Since you're not controlling the exact process of deserialization (RestEasy does) - a first option would be to simply inject the JSON as a String and then take control of the deserialization process:

Collection<COrder> readValues = new ObjectMapper().readValue(jsonAsString, new TypeReference<Collection<COrder>>() { });

你可以放松一些不必自己做的便利,但是你很容易排序出问题。

You would loose a bit of the convenience of not having to do that yourself, but you would easily sort out the problem.

另一个选项 - 如果你不能改变JSON - 将构造一个包装器以适应你的JSON输入的结构 - 并使用它而不是 Collection< COrder>

Another option - if you cannot change the JSON - would be to construct a wrapper to fit the structure of your JSON input - and use that instead of Collection<COrder>.

希望这会有所帮助。