将JsonNode转换为Java POJO。

huangapple go评论73阅读模式
英文:

Convert JsonNode into Java POJO

问题

HttpResponse<JsonNode> response = Unirest.post("https://json.islandia.com/v1/martorell")
.basicAuth("624823", "8f1addd21a09d6b95eaefa8d60p4c05")
.field("day", "28")
.asJson();

Root newJsonNode = new ObjectMapper().treeToValue(response.getBody(), Root.class);
但是我遇到了这个错误:Cannot resolve method &#39;treeToValue(JsonNode, Class&lt;Root&gt;)&#39;

英文:

I have this piece of code:

 HttpResponse&lt;JsonNode&gt; response = Unirest.post(&quot;https://json.islandia.com/v1/martorell&quot;)
                .basicAuth(&quot;624823&quot;, &quot;8f1addd21a09d6b95eaefa8d60p4c05&quot;)
                .field(&quot;day&quot;, &quot;28&quot;)
                .asJson();

        Root newJsonNode = new ObjectMapper().treeToValue(response.getBody(), Root.class);

but I have this error: Cannot resolve method &#39;treeToValue(JsonNode, Class&lt;Root&gt;)&#39;

答案1

得分: 1

考虑到 ObjectMapper#treeToValue() 存在,请检查您文件开头的导入部分:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

如果您没有看到这些导入语句,这可能解释了错误消息。

确保 Jackson 库 已正确添加到您的项目的类路径中。您可以在 Maven 的 pom.xml 中添加以下依赖项:

<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.12.1</version>
    </dependency>
    <dependency>
        <groupId>com.mashape.unirest</groupId>
        <artifactId>unirest-java</artifactId>
        <version>1.4.9</version>
    </dependency>
</dependencies>

如果尝试使用 convertValue 而出现问题,您会得到以下错误消息:

找不到类 java.io.ByteArrayInputStream 的序列化程序,也没有发现属性以创建 BeanSerializer(为了避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)

因此,您正尝试序列化一个 ByteArrayInputStream 对象,Jackson 默认不知道如何序列化它。

HttpResponse.getBody() 可能 返回 JsonNode 类型,而是返回一个需要在您可以使用 Jackson 的转换功能之前手动转换为 JsonNode 的 InputStream。

例如:

HttpResponse<InputStream> response = Unirest.post("https://json.islandia.com/v1/martorell")
                .basicAuth("624823", "8f1addd21a09d6b95eaefa8d60p4c05")
                .field("day", "28")
                .asObject(InputStream.class);

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(response.getBody());
Root newJsonNode = mapper.treeToValue(jsonNode, Root.class);

在这里,HTTP 响应的正文是一个 InputStream,首先使用 ObjectMapper.readTree() 将其转换为 JsonNode 对象。然后 使用 ObjectMapper.treeToValue() 将 JsonNode 转换为 Root 对象。Root 类应该是与您正在处理的 JSON 结构匹配的 POJO。如果不匹配,可能会遇到更多错误。

英文:

Considering ObjectMapper#treeToValue() does exist, check your import at the begginning of your file:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

If you do not see those imports, that could explain the error message.

Make sure that the Jackson libraries are properly added to your project's classpath. You can add the following dependencies in your Maven's pom.xml:

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
        &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
        &lt;version&gt;2.12.1&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.mashape.unirest&lt;/groupId&gt;
        &lt;artifactId&gt;unirest-java&lt;/artifactId&gt;
        &lt;version&gt;1.4.9&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

> If I try using convertValue instead, I get:
>
>
&gt; No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, &gt; disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
&gt;

So you are trying to serialize a ByteArrayInputStream object, which Jackson does not know how to serialize by default.

HttpResponse.getBody() might not be returning a JsonNode type: instead, it could be returning an InputStream which needs to be manually converted to JsonNode before you can utilize Jackson's conversion feature.

For instance:

HttpResponse&lt;InputStream&gt; response = Unirest.post(&quot;https://json.islandia.com/v1/martorell&quot;)
                .basicAuth(&quot;624823&quot;, &quot;8f1addd21a09d6b95eaefa8d60p4c05&quot;)
                .field(&quot;day&quot;, &quot;28&quot;)
                .asObject(InputStream.class);

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(response.getBody());
Root newJsonNode = mapper.treeToValue(jsonNode, Root.class);

Here, the body of the HTTP response, which is an InputStream, is first converted to a JsonNode object using ObjectMapper.readTree(). Then, the JsonNode is converted to a Root object using ObjectMapper.treeToValue(). The Root class should be a POJO that matches the structure of the JSON you are working with. If it does not, you might run into more errors.

huangapple
  • 本文由 发表于 2023年7月18日 01:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定