java - 如何将 json 响应字段更改为 java 中的用户定义字段?

标签 java json jackson

我有以下 json 响应:

{
"Labels": {
                     "com.prop.vendor": "Acme",
                     "com.example.license": "GPL",
                     "com.example.version": "1.0"
             }
}

现在我想在我的 java 类中处理这些值。我遇到的问题是对于像 com.example.version 这样的字段,pojo 是使用与数据类型相同的方式生成的。喜欢,

private String com.example.version

这给了我编译时错误。 所以,我想创建一个指向“com.example.version”的字段“String version” 我已经尝试使用@JsonProperty 和@JsonSetter。

Labels.java

public class Labels
{
    @JsonProperty("com.example.version")
    private String version;

    @JsonProperty("com.example.vendor")
    private String vendor;

    @JsonProperty("com.example.license")
    private String license;

    public String getVersion()
    {
        return version;
    }

    public void setVersion(String version)
    {
        this.version = version;
    }

    public String getVendor()
    {
        return vendor;
    }

    public void setVendor(String vendor)
    {
        this.vendor = vendor;
    }

    public String getLicense()
    {
        return license;
    }

    public void setLicense(String license)
    {
        this.license = license;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [com.example.version = " + version + ", com.example.vendor = " + vendor + ", com.example.license = "
                + license + "]";
    }
}

但是每次我都会收到这个错误:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Labels" (class com.csc.agility.adapters.service.docker.pojo.Labels), not marked as ignorable (3 known properties: "com.example.license", "com.example.vendor", "com.example.version"])
 at [Source: {
"Labels": {
                     "com.example.vendor": "Acme",
                     "com.example.license": "GPL",
                     "com.example.version": "1.0"
             }
}; line: 2, column: 12] (through reference chain: com.csc.agility.adapters.service.docker.pojo.Labels["Labels"])

我是如何使用它的:

ObjectMapper objMap = new ObjectMapper();
String jsonInString =
                "{\n\"Labels\": {\n                     \"com.example.vendor\": \"Acme\",\n                     \"com.example.license\": \"GPL\",\n                     \"com.example.version\": \"1.0\"\n             }\n}";

Label label = objMap.readValue(jsonInString, Label.class);
String licence = label.getLicense();

任何人都可以指出要使用的正确 json 注释或任何其他方式来实现它吗?

最佳答案

您应该使用

配置您的 mapper

mapper.configure(Feature.UNWRAP_ROOT_VALUE, true);(适用于 1.9 版本)

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);(适用于版本 2 及更高版本)。

我已经用上面的属性尝试了你的 json,一切正常:

String data = "{\"Labels\": {\"com.example.vendor\": \"Acme\",\"com.example.license\": \"GPL\",\"com.example.version\": \"1.0\"}}";

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
Labels labels = mapper.readValue(data, Labels.class);
System.out.println(labels);

注意:在 Json 字符串中,我已将 com.prop.vendor 更改为 com.example.vendor 以使其可用。

关于java - 如何将 json 响应字段更改为 java 中的用户定义字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40216144/

相关文章:

java - Spring MVC 的问题。如何从两个或多个对象创建 View ?

java - Kotlin 自定义注释、参数

php - Web服务API设计中哪种schema更好

php - 错误未定义属性 : stdClass while trying to parse json file in php

java - Spring正确调用属性文件

java - 是否可以在 JDBC 中批量创建 Oracle Struct?

javascript - 在 NodeJS api 服务器中从客户端获取 JSON 内容

Spring和jackson,如何通过@ResponseBody禁用FAIL_ON_EMPTY_BEANS

java - 子类化列表和 Jackson JSON 序列化

java - jackson 为什么我需要在子类上进行 JsonTypeName 注释