- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个问题。我使用自定义注释,它采用方法参数并将它们从 json 转换为 pojo。这是示例:
...
MethodParameter param
// here a parameter from methods, marked with custom annotation
...
JSON_MAPPER.readValue(node, param.getParameterType());
java.util.LinkedHashMap cannot be cast to com.liferay.portal.kernel.util.KeyValuePair
但是当我尝试转换 List<> 时它不起作用。但下面的代码工作正常
JSON_MAPPER.readValue(node, new TypeReference<List<KeyValuePair>>(){});
如何找出收入数据的类型?我该怎么办?
最佳答案
我假设你的方法看起来像
public List<KeyValuePair> methodName(..) {..}
换句话说,它的返回类型就是您希望 JSON 解析为的类型。
我还将假设 MethodParameter
是 org.springframework.core.MethodParameter
。您会注意到 MethodParameter#getParameterType()
返回 Class<?>
。在这种情况下,它将返回 Class
List
的对象类型。
给定 List.class
, jackson 无法猜测元素类型是什么。因此,它使用默认值 LinkedHashMap
。
使用 MethodParameter#getGenericParameterType()
方法将返回 Type
对象完全描述List<KeyValuePair>
返回类型。 Jackson 将从中获得足够的类型信息,以便从 JSON 构建适当的对象。
您需要转换 Type
进入JavaType
预计 ObjectMapper#readValue
.
mapper.readValue(node, mapper.getTypeFactory().constructType(param.getGenericParameterType()));
关于java - Jackson ReadValue 不会转换为 List<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32743105/
我正在尝试将 json api 响应映射到对象,而 IntelliJ 正在提示。它说无法解析方法readValue(java.lang.String, java.lang.Object[]);。我意识
我在我的应用程序中使用 Jackson 进行反/序列化。 我遇到了一种情况,需要将 JSON 字符串转换为我的 3 个类之一。如果字符串无法转换为 3 个类中的任何一个,则会被视为无法识别的情况。 但
Jackson 库中的 ObjectMapper.readValue 确实返回空值。在什么情况下它返回空对象?它从代码库中发现了这一点。 org.codehaus.jackson j
@Service public class PokemonManager implements PokemonService { private HttpResponse getStringH
我有一个问题。我使用自定义注释,它采用方法参数并将它们从 json 转换为 pojo。这是示例: ... MethodParameter param // here a parameter from
在对象映射器类中,当我将参数传递为“abc,asd”(用于 Junit 测试目的)时,我收到 JsonParseException 。请澄清为什么会出现此异常。它会接受什么值以进行进一步处理。 Obj
我正在编写以下代码来将我的 JSON 字符串转换为我的对象列表。 List myResponse = new ArrayList(); myResponse = new ObjectMapper().
我正在查看有关将 Java 对象转换为 JSON 的代码示例,我遇到了这个: HashMap filters = new ObjectMapper().readValue(filterStr, Has
我正在使用 Java/Spring 创建简单的休息客户端。我的请求已被远程服务正确使用,并且我收到了响应 String 某些内容: {"access_token":"d1c9ae1b-bf21-4b8
我正在测试服务层,但不确定如何模拟 ObjectMapper().readValue在那个类(class)。我对 mockito 还很陌生并且可以弄清楚该怎么做。 以下是我的代码, 服务.java p
我有一个包含 JSONObjects 的 org.json.JSONArray,我正在尝试将它们映射到 POJO。我知道要映射到的 POJO 的类型。我有 2 个选项,我正在尝试找出哪个在性能上更好。
有一个关于 Mockito 的类似问题 here 我有一种情况,我想模拟 readValue在下一行 val animal: Animal = objectMapper.readValue(Stri
我正在尝试为我的值类构建自定义反序列化器类。但是 ObjectMapper .readValue 会被多次调用,直到抛出 StackOverflowException 为止。以下仅为示例: @Json
我在尝试将 json 字符串转换为对象时看到下面列出的错误。我没有数据类的源代码,它是 jar 文件的一部分。有什么方法可以使用 mixin 来解决这个问题吗? 代码 ObjectMapper map
我需要解析这个json: { "id":"cpd4-b39c4b2a-b5cb-4581-b519-6034aaa7fe4e", "transactionId":"768a9be4-b5b
我正在尝试模拟 MAPPER.readValue() 但无法获得预期的输出。 private static final ObjectMapper MAPPER = new ObjectMapper()
我有以下 json 数据 (patients.json): { "A" : { "name" : "Tom", "age" : 12 }, "B
我决定通过实现 Json.Serialized 接口(interface)来编写自己的读写对象方法,因为我对 Json 的自动对象写入方式(它省略了数组)不满意。我的写入方法工作正常,但是由于某种原因
我在下面有一个语句,它为我提供了类型为 LinkedHashMap 的对象列表。 List optionsList = mapper.readValue(mapper.writeValueAsStri
本文整理了Java中org.crazyyak.dev.jackson.YakJacksonObjectMapper.readValue()方法的一些代码示例,展示了YakJacksonObjectMa
我是一名优秀的程序员,十分优秀!