尝试在 Java 中使用 Jackson 时出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4392326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:54:41  来源:igfitidea点击:

Issue when trying to use Hymanson in java

javaHymanson

提问by eddy

I'm trying to use Hymanson to convert some JSON data into Java objects ,a list of objects to be precise,but I get this error:

我正在尝试使用 Hymanson 将一些 JSON 数据转换为 Java 对象,准确地说是一个对象列表,但出现此错误:

org.codehaus.Hymanson.map.JsonMappingException: Can not deserialize instance of entitylayer.Detail out of START_ARRAY token

org.codehaus.Hymanson.map.JsonMappingException:无法从 START_ARRAY 令牌中反序列化 entitylayer.Detail 的实例

this is the code:

这是代码:

 ObjectMapper mapper = new ObjectMapper(); 
 List<Detail> lcd = (List<Detail>) mapper.readValue(ld, Detail.class);

ldis the list in Json format, this is the part that makes me comfused in the Hymanson tutorial. what does new File("user.json")represent? I assumed that was the string in json format I wanted to convert, that's why I used ld.

ld是 Json 格式的列表,这是让我在Hymanson 教程中感到困惑的部分。这是什么新的文件(“user.json”)代表什么?我认为这是我想转换的 json 格式的字符串,这就是我使用ld的原因。

I hope you can help me out with it

我希望你能帮我解决这个问题

采纳答案by OrangeDog

From the tutorial you linked (other Collections work the same way):

从您链接的教程(其他集合的工作方式相同):

So if you want to bind data into a Map you will need to use:

Map<String,User> result = mapper.readValue(src, new TypeReference<Map<String,User>>() { });

where TypeReference is only needed to pass generic type definition (via anynomous inner class in this case): the important part is > which defines type to bind to.

If you don't do this (and just pass Map.class), call is equivalent to binding to Map (i.e. "untyped" Map), as explained above.

因此,如果要将数据绑定到 Map 中,则需要使用:

Map<String,User> result = mapper.readValue(src, new TypeReference<Map<String,User>>() { });

其中 TypeReference 只需要传递泛型类型定义(在这种情况下通过任意内部类):重要的部分是 > 定义要绑定到的类型。

如果你不这样做(只是传递 Map.class),调用就相当于绑定到 Map(即“无类型”Map),如上所述。

Edit:

编辑:

If you insist on being spoon fed:

如果你坚持用勺子喂食:

List<Detail> lcd = mapper.readValue(ld, new TypeReference<List<Detail>>() {});

回答by StaxMan

As an alternative strategy, dealing with arrays is slightly more convenient, because you can easily specify type. So alternatively you could also do this:

作为替代策略,处理数组稍微方便一些,因为您可以轻松指定类型。因此,您也可以这样做:

Detail[] details = mapper.readValue(ld, Detail[].class);

Arrays are often underused, at least when dealing with Hymanson. As long as you don't have to modify results too much they are very convenient to use, since types are nicely contained without needing to use generics (and as result, type-safety is strong; it's not just compiler fluff).

数组经常未得到充分利用,至少在与 Hymanson 打交道时是这样。只要您不必过多地修改结果,它们就非常方便使用,因为类型被很好地包含而无需使用泛型(因此,类型安全性很强;它不仅仅是编译器绒毛)。

回答by Gautam

Alternatively parse the JSON yourself and create whatever POJOs you wish to create. I did that to transform JSON to JAXB.

或者自己解析 JSON 并创建您希望创建的任何 POJO。我这样做是为了将 JSON 转换为 JAXB。

Refer to the "Streaming API Example" section at http://wiki.fasterxml.com/HymansonInFiveMinutes

请参阅http://wiki.fasterxml.com/HymansonInFiveMinutes上的“流 API 示例”部分

Its easier than you would expect it to be.

它比您预期的要容易。

回答by user2319311

Whenever you get this error, first check if the JSON string you are trying to convert to is a proper JSON string. If you try to create your own JSON string for local testing your code then you end up in getting this error.

每当您收到此错误时,首先检查您尝试转换为的 JSON 字符串是否是正确的 JSON 字符串。如果您尝试创建自己的 JSON 字符串用于本地测试您的代码,那么您最终会收到此错误。