使用Jackson绑定带有列表的json和一个对象

时间:2013-12-27 01:41:44

标签: java json data-binding jackson

鉴于我有以下json:

{
    "Company": {
        "name": "cookieltd",
        "type": "food",
        "franchise_location": [
            {
                "location_type": "town",
                "address_1": "5street"
            },
            {
                "location_type": "village",
                "address_1": "2road"
            }
        ]
    }
}

如何使用Jackson绑定到以下对象类?:

1)公司类

public class Company
{
    String name, type;
    List<Location> franchise_location = new ArrayList<Location>();

    [getters and setters]
}

2)位置等级

public class Location
{
    String location_type, address_1;

    [getters and setters]

}

我做了:

String content = [json above];
ObjectReader reader = mapper.reader(Company.class).withRootName("Company"); //read after the root name
Company company = reader.readValue(content);

但我得到了:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "franchise_location"

2 个答案:

答案 0 :(得分:0)

据我所知,你只是错过了一个适当命名的字段franchise_location的getter。它应该是

public List<Location> getFranchise_location() {
    return franchise_location;
}

(和二传手)

public void setFranchise_location(List<Location> franchise_location) {
    this.franchise_location = franchise_location;
}

或者,您可以使用

注释当前的getter或字段
@JsonProperty("franchise_location")
private List<Location> franchiseLocation = ...;

有助于映射那些与Java字段名称约定无关的JSON元素名称。


以下为我工作

public static void main(String[] args) throws Exception {
    String json = "{ \"Company\": { \"name\": \"cookieltd\", \"type\": \"food\", \"franchise_location\": [ { \"location_type\": \"town\", \"address_1\": \"5street\" }, { \"location_type\": \"village\", \"address_1\": \"2road\" } ] } }";
    ObjectMapper mapper = new ObjectMapper();
    ObjectReader reader = mapper.reader(Company.class).withRootName(
            "Company"); // read after the root name
    Company company = reader.readValue(json);
    System.out.println(company.getFranchise_location().get(0).getAddress_1());
}

public static class Company {
    private String name;
    private String type;
    private List<Location> franchise_location = new ArrayList<Location>();

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public List<Location> getFranchise_location() {
        return franchise_location;
    }
    public void setFranchise_location(List<Location> franchise_location) {
        this.franchise_location = franchise_location;
    }
}

public static class Location {
    private String location_type;
    private String address_1;
    public String getLocation_type() {
        return location_type;
    }
    public void setLocation_type(String location_type) {
        this.location_type = location_type;
    }
    public String getAddress_1() {
        return address_1;
    }
    public void setAddress_1(String address_1) {
        this.address_1 = address_1;
    }
}

并打印

5street

答案 1 :(得分:-2)

我的JSON解决方案始终是GSON,你可以对此做一些研究,只要你根据JSON拥有正确的类结构,它就可以自动从JSON转移到对象:
Company company = gson.fromJson(json, Company.class);
GSON是如此聪明地做转换的事情! 享受GSON!