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

时间:2017-11-16 12:37:20

标签: java json class jackson deserialization

我有一个以下格式的json:

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

如您所见,firstName字段中存在的四个字段lastNamemiddleNamesuffixalias也位于其上方作为父类的字段。

目前我有以下课程:

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;
}

它与杰克逊映射器配合使用。但我的问题是,可以将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中编写一个deserialiser,它将映射json的字段firstNamelastNamemiddleNamesuffix以加载到新变量中{ {1}}?

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

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

enter image description here

这就是我想要的:

enter image description here

1 个答案:

答案 0 :(得分:3)

为了解决问题,@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;
    }
}