将单个JSON对象映射到三个不同的JAVA类?

时间:2014-07-10 11:25:51

标签: java json

任何人都可以帮助我,我是JSON的新手。问题是我想将一个json对象映射到Keys值对三个不同的java类?

{"firstName":"Sasi","lastName":"Dunston","jobTitle":"Trainee","dateOfBirth":"13/09/1990","bloodGroup":"O+ve",

             "listOfEmail":["dunston08@gmail.com","charles.gmail.com","ravi.gmail.com"],
             "listOfURL":["www.google.com","www.gmail.com","www.facebook.com"],
             "listOfFaxNo":[8888888888,1111111111,2222222222,3333333333],
             "listOfOfficeNo":[9999999999,8888888888,7777777777,6666666666],
             "listOfAddress":[{"streetName":"xxxxx","city":"yyyyy","zipCode":"5555555","state":"hhhhhhh"},                        
                              {"streetName":"xxxxx","city":"yyyyy","zipCode":"5555555","state":"hhhhhhh"}]

}

这是我的json对象,我想将它映射到三个不同的类

Class PersonDetail
{
         firstName 
         lastName
         jobTitle
         dateOfBirth
         bloodGroup
/* Getter Setter */ of the above attributes
}
class PersonContact extends PersonDetail
{
    ArrayList<String> listOfEmail=new ArrayList<String>();
    ArrayList<String> listOfURL=new ArrayList<String>();
    ArrayList<String> listOfFaxNo=new ArrayList<String>();
    ArrayList<String> listOfPhoneNo=new ArrayList<String>();
    /* Getter Setter */ of the above attributes
}
class Address extends PersonContact
{
    String streetName;
    String city;
    String zipcode;
    String state;
/* Getter Setter */ of the above attributes
}

2 个答案:

答案 0 :(得分:0)

只需使用https://github.com/FasterXML/jackson-databind

即可

本教程应该允许您将相同的源映射到不同的Java对象(类)

答案 1 :(得分:0)

未经测试,但您应该能够使用以下类定义将json映射到PersonContact

Class PersonDetail
{
    protected String firstName;
    protected String  lastName;
    protected String  jobTitle;
    protected String  dateOfBirth;
    protected String  bloodGroup;
    /* Getter Setter */ of the above attributes
}
class PersonContact extends PersonDetail
{
    private ArrayList<String> listOfEmail;
    private ArrayList<String> listOfURL;
    private ArrayList<String> listOfFaxNo;
    private ArrayList<String> listOfOfficeNo;
    private List<Address> listOfAddress;
    /* Getter Setter */ of the above attributes
}
class Address 
{
    private String streetName;
    private String city;
    private String zipcode;
    private String state;
    /* Getter Setter */ of the above attributes
}
相关问题