杰克逊映射json数组键值对pojo

时间:2017-07-19 06:38:54

标签: java json jackson

如何使用jackson将以下json转换为pojo类。

{
"company": [{
    "employee": {
        "address": {
            "email": "abc@gmail.com"
        },
        "info": {
            "role": "admin",
            "name": "abc"
        }
    }
},
{
    "employee": {
        "address": {
            "email": "xyz@gmail.com"
        },
        "info": {
            "role": "developer",
            "name": "xyz"
        }
    }
}]
}

示例Java代码(它将包括getter和setter)

public class Company{
@JsonProperty("employee")
List<Employee> employee;

}

1 个答案:

答案 0 :(得分:0)

我可以帮助你。

你必须创建相当于这个jason权限的pojo类。我们走了

与你的json相当的pojo类如下所述

<强> Employee.java

public class Employee
{
    private Address address;

    private Info info;

    public Address getAddress ()
    {
        return address;
    }

    public void setAddress (Address address)
    {
        this.address = address;
    }

    public Info getInfo ()
    {
        return info;
    }

    public void setInfo (Info info)
    {
        this.info = info;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [address = "+address+", info = "+info+"]";
    }
}

<强> Address.java

public class Address
{
    private String email;

    public String getEmail ()
    {
        return email;
    }

    public void setEmail (String email)
    {
        this.email = email;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [email = "+email+"]";
    }
}

<强> Company.java

public class Company
{
    private Employee employee;

    public Employee getEmployee ()
    {
        return employee;
    }

    public void setEmployee (Employee employee)
    {
        this.employee = employee;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [employee = "+employee+"]";
    }
}

<强> Info.java

public class Info
{
    private String name;

    private String role;

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    public String getRole ()
    {
        return role;
    }

    public void setRole (String role)
    {
        this.role = role;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [name = "+name+", role = "+role+"]";
    }
}