简体   繁体   English

如何使用 Jackson 的 ObjectMapper 从 JSON 字符串创建 object?

[英]How do I create an object from a JSON string using Jackson's ObjectMapper?

I have classes structured in the following way:我的课程结构如下:

@JsonRootName("class1")
public class class1 {
    private interface1 foo;

    public interface1 getFoo() {
        return foo;
    }

    public void setFoo(interface1 foo) {
        this.foo = foo;
    }
}
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonSubTypes({@JsonSubTypes.Type(value = class2.class, name = "class2")})
public interface interface1 {
    String getType();
}
@JsonTypeName("class2")
public class class2 implements interface1 {
    private String buzz = "bar";

    @Override
    public String getType() {
        return "class2";
    }

    public String getBuzz() {
        return buzz;
    }

    public void setBuzz(String buzz) {
        this.buzz = buzz;
    }
}

If I parsed the following JSON into class1 using the ObjectMapper it would be fine:如果我使用ObjectMapper将以下 JSON 解析为 class1 就可以了:

{
    "class1": {
        "foo": {
            "class2": {
                "buzz": "not bar"
            }
        }
    }
}

In this example the buzz variable in the class2 object would be set to "not bar" and the foo variable in the class1 would be set to the class2 object.在此示例中,class2 object 中的 Buzz 变量将设置为“not bar”,而 class1 中的 foo 变量将设置为 class2 object。

If I didn't want to modify the buzz variable in class2 and just wanted the foo variable in class1 to be set to a plain class2 object, how would I do that?如果我不想修改class2中的buzz变量,而只想将class1中的 foo 变量设置为普通的class2 object,我该怎么做? The following doesn't work since "class2" is interpreted as a string:由于"class2"被解释为字符串,因此以下内容不起作用:

{
    "class1": {
        "foo": "class2"
    }
}

Same as the first example, but without the buzz field.与第一个示例相同,但没有buzz字段。

{
    "class1": {
        "foo": {
            "class2": {
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用Jackson解析下面的JSON。 使用Jackson ObjectMapper创建的最相关的java对象是什么? - How do I parse the below JSON with Jackson. What is the most relevant java object to create for using Jackson ObjectMapper? 如何使用 jackson ObjectMapper 解析 Mongodb 的 UUID 对象? - How do I parse a Mongodb's UUID object using jackson ObjectMapper? 如何从Jackson JSON中的ObjectMapper直接写入JSON对象(ObjectNode)? - How to directly write to a JSON object (ObjectNode) from ObjectMapper in Jackson JSON? 使用杰克逊(ObjectMapper)如何将对象序列化为json并忽略除我注释为@JsonProperty的字段以外的所有其他字段? - Using Jackson (ObjectMapper) how serialize object to json and ignore all fields except the ones I annotate as @JsonProperty? 使用 Jackson 的 ObjectMapper 的 JSON 对象顺序 - Order of JSON objects using Jackson's ObjectMapper 如何使用Kotlin和杰克逊ObjectMapper从json中删除属性 - how to remove attributes from json using Kotlin and jackson ObjectMapper 如何正确重用Jackson ObjectMapper? - How do I correctly reuse Jackson ObjectMapper? 使用Jackson的ObjectMapper解析具有动态类型(字符串或对象)值的JSON - Parsing JSON with a value with dynamic type (either string or object) with Jackson's ObjectMapper 如何使用Java和Jackson库对json字符串进行反序列化,其中对象的字段的子类包含在json字符串中 - How can i deserialize Json string where object's field's subclass is included in json string, using Java and Jackson library 如何使用 Jackson 的 object 映射器解析 JSON 字符串以删除双引号? - How can I parse the JSON string using Jackson's object mapper to remove the double quotes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM