代码之家  ›  专栏  ›  技术社区  ›  Vivek Kumar

将json中的某些字段反序列化为其他类

  •  -1
  • Vivek Kumar  · 技术社区  · 6 年前

    我有以下格式的json:

    {
        "id": 0,
        "alternateId": 0,
        "familyName": "",
        "givenName": "",
        "firstName":"",
        "lastName": "",
        "middleName": "",
        "suffix": "",    
        "motherName": "",
        "datOfBirth": "",
        "sex": "",
        "alias": [
            {
            "firstName": "",
            "lastName": "",
            "middleName": "",
            "suffix": "",
            }
        ]
    }
    

    firstName , lastName , middleName suffix 存在于 alias 字段也作为父类的字段存在于其上方。

    目前我有以下课程:

    public class Person
    {
        private int     id;
        private int     alternateId;
        private String  familyName;
        private String  givenName;
        private String  firstName;
        private String  lastName;
        private String  middleName;
        private String  suffix;
        private String  motherName;
        private Date    datOfBirth;
        private String  sex;
        private Set<Alias>  alias;
    }
    
    public class Alias
    {
        private String  firstName;
        private String  lastName;
        private String  middleName;
        private String  suffix;
    }
    

    和jackson mapper一起工作很好。但我的问题是,是否可以将Person类中的字段合并为一个新字段primaryAlias,并将其类缩减为:

    public class Person
    {
        private int     id;
        private int     alternateId;
        private String  familyName;
        private String  givenName;
        private Alias   primaryAlias;
        private String  motherName;
        private Date    datOfBirth;
        private String  sex;
        private Set<Alias>  alias;
    }
    

    不更改当前json。可以在jackson中编写一个反序列化程序来映射字段吗 名字 , 姓氏 , 中间名 后缀 要加载到新变量中的json的 primaryAlias ?

    这样做不需要完整的代码,但是提示一下该做什么就足够了。我试着搜索,但可能我错过了描述这个问题的完美术语。

    也许我可以用图像更好地解释。这就是目前正在发生的事情:

    enter image description here

    这就是我想要的:

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  3
  •   Optional    6 年前

    来救你吧 @JsonCreator 它在创建类之前为您提供控制,以便您可以自己填充属性:)

    这是我的快速工作(我刚刚使用了大json的有限属性)。类似地,我的toString方法只是反映加载了属性。如果你愿意,你可以有一个有意义的toString。

    import com.fasterxml.jackson.annotation.JsonCreator;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import java.util.Map;
    import java.util.Set;
    
    public class JacksonTest {
    
        final static ObjectMapper mapper = new ObjectMapper();
    
        public static void main(String[] args) throws Exception {
    
            final String jsonInString = "{\n"
                    + "    \"id\": 0,\n"
                    + "    \"familyName\": \"someFN\",\n"
                    + "    \"givenName\": \"someGivenName\",\n"
                    + "    \"firstName\":\"firstNameInperson\",\n"
                    + "    \"lastName\": \"\",\n"
                    + "    \"middleName\": \"\",\n"
                    + "    \"suffix\": \"\",    \n"
                    + "    \"motherName\": \"\",\n"
                    + "    \"alias\": [\n"
                    + "        {\n"
                    + "        \"firstName\": \"firstNameInAlias\",\n"
                    + "        \"lastName\": \"\",\n"
                    + "        \"middleName\": \"\",\n"
                    + "        \"suffix\": \"\"\n"
                    + "        }\n"
                    + "    ]\n"
                    + "}";
            //System.out.println(jsonInString);
    
            Person person = mapper.readValue(jsonInString, Person.class);
            System.out.println(person);
            // System.out.println(newFoo.getSomething());
        }
    }
    
    class Person {
    
        @JsonProperty
        private int id;
    
        @JsonProperty
        private String familyName;
    
        Alias alias;
    
        @JsonProperty
        private String motherName;
        @JsonProperty
        private Set<Alias> aliases;
    
        @JsonCreator
        public Person(final Map<String, Object> properties) {
            System.out.println("printing in constructor.." + properties);
            id = (Integer) properties.get("id");
            familyName = (String) properties.get("familyName");
            Alias al = new Alias();
            al.firstName = (String) properties.get("firstName");
            al.lastName = (String) properties.get("lastName");
            al.middleName = (String) properties.get("middleName");
            al.suffix = (String) properties.get("suffix");
            this.alias = al;
            TypeReference aliasesType = new TypeReference<Set<Alias>>() {
            };
            try {
                aliases = JacksonTest.mapper.readValue(JacksonTest.mapper.writeValueAsString(properties.get("alias")), aliasesType);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
        public String toString() {
            return "Person: familyName:" + familyName + ", Alias: " + alias + " Aliases:" + aliases;
        }
    
    }
    
    class Alias {
    
        @JsonProperty
        String firstName;
    
        @JsonProperty
        String lastName;
    
        @JsonProperty
        String middleName;
    
        @JsonProperty
        String suffix;
    
        public String toString() {
            return "Alias : firstName::" + firstName;
        }
    }