简体   繁体   English

将JSON反序列化为Java对象时,如何将父属性映射到子对象?

[英]How to map a parent attribute to a child object when deserializing JSON into Java objects?

Given JSON like this: 给定这样的JSON:

{
    "locale" : "US",
    "children" : [
            {
                "foo" : "bar"
            },
            {
                "foo" : "baz"
            }
        ]
}

being mapped to Java objects like this: 像这样映射到Java对象:

public class Parent {
    @JsonProperty public String getLocale() {...}
    @JsonProperty public List<Child> getChildren() {...}
}

public class Child {
    public void setLocale(String locale) {...}
    @JsonProperty public String getFoo() {...}
}

How can I populate the locale property of Child instances with the value that is in the JSON at the top ( Parent ) level? 如何使用顶级( Parent )级别的JSON中的值填充Child实例的locale属性?

I thought I might be able to use @JsonDeserialize(using=MyDeserializer.class) on the setLocale() method of Child to use a custom serializer, but that's not working (I suspect because there is no value in the JSON at the Child level so Jackson doesn't know about any values that are supposed to be deserialized into the locale property). 我以为我可能可以在ChildsetLocale()方法上使用@JsonDeserialize(using=MyDeserializer.class)以使用自定义序列化程序,但这无法正常工作(我怀疑是因为Child级别的JSON中没有值)因此,杰克逊不知道应该反序列化到locale属性中的任何值)。

I'd like to avoid having to write an entire custom deserializer for the entire Child class, which in reality has lots more data to be mapped. 我想避免为整个Child类编写整个自定义反序列化器,实际上,该类有许多要映射的数据。

If it is acceptable to have a reference to a parent in a child object, then you can use bi-directional references to establish the parent-child relationship between you classes. 如果在子对象中具有对父对象的引用是可以接受的,则可以使用双向引用在类之间建立父子关系。 Here is an example: 这是一个例子:

public class JacksonParentChild {
    public static class Parent {
        public String locale;
        @JsonManagedReference
        public List<Child> children;

        @Override
        public String toString() {
            return "Parent{" +
                    "locale='" + locale + '\'' +
                    ", children=" + children +
                    '}';
        }
    }

    public static class Child {
        @JsonBackReference
        public Parent parent;
        public String foo;

        @Override
        public String toString() {
            return "Child{" +
                    "locale='" + parent.locale + '\'' +
                    ", foo='" + foo + '\'' +
                    '}';
        }
    }

    final static String json = "{\n" +
            "    \"locale\" : \"US\",\n" +
            "    \"children\" : [\n" +
            "            {\n" +
            "                \"foo\" : \"bar\"\n" +
            "            },\n" +
            "            {\n" +
            "                \"foo\" : \"baz\"\n" +
            "            }\n" +
            "        ]\n" +
            "}";

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Parent parent = mapper.readValue(json, Parent.class);
        System.out.println("Dumping the object");
        System.out.println(parent);
        System.out.println("Serializing to JSON");
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(parent));
    }
}

Output: 输出:

Dumping the object:
Parent{locale='US', children=[Child{locale='US', foo='bar'}, Child{locale='US', foo='baz'}]}
Serializing to JSON:
{
  "locale" : "US",
  "children" : [ {
    "foo" : "bar"
  }, {
    "foo" : "baz"
  } ]
}

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

相关问题 在单个Java对象中反序列化JSON的父级和子级节点数据 - Deserializing parent and child node data of json in a single java object Java:将JSON结构反序列化为Map <String, Object> - Java: Deserializing JSON structure to Map<String, Object> 将子 object 从 JSON 绑定到父属性 java - binding a child object from JSON to parent attribute java 如何将Json转换为Java对象,反序列化Json - How to convert Json to Java Object, Deserializing Json 与Jackson反序列化时,重用父对象的值来构造子对象 - Reuse value from parent object to construct child when deserializing with Jackson 使用列表属性将JSON反序列化为Object - Deserializing JSON into Object with list attribute 使用hibernate jpa进行JSON序列化和反序列化,以便在JSON响应中将父对象转换为子对象 - JSON serializing and deserializing with hibernate jpa to have parent object into child in JSON response 子 Object 有父 Object 作为属性 JPA 导致无穷无尽的 Z0ECD11C1D7A287401F8D14A7A - Child Object has parent Object as attribute JPA caused endless JSON 在Java中将子对象用作父对象属性是不好的做法吗? - Is it bad practice to use child object as a parent object attribute in Java? 当我克隆父 Object 时,它会修改原始父的子对象 - When I clone a Parent Object, it modifies the child objects of original Parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM