Json Array到Pojo [英] Json Array to Pojo

查看:139
本文介绍了Json Array到Pojo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将以下Json数组响应解组为RestTemplate中的pojos集合。

I'm trying to unmarshal the following Json array response to a collection of pojos in RestTemplate.

[{
  "client":{
        "id": 6364,
        "name": "7Seven7 Insurance Inc",
        "email": "donna@7seven7ins.com",
        "currency": {"name":"United States of America, Dollars","symbol":"$"},
        "address": "941 Elm Ave. #5 ",
        "city": "Long Beach",
        "province": "CA",
        "zip_code": "90813",
        "country": "United States",
        "full_address_with_comma": "941 Elm Ave. #5, Long Beach, CA, 90813, United States",
        "phone": "562-556-4035",
        "fax":"562-381-7500",
        "custom_field_name": "listed",
        "custom_field_value": "false",
        "created_at": "2010-07-18T00:08:10Z",
        "updated_at": "2010-07-21T11:04:58Z",
      }
},
{
  "client":{
        "id":6365,
        "name": "Affinity",
        "email":"CGregory@affinitygroup.com",
        "address":"2575 Vista Del Mar ",
        "city":"Ventura",
        "province":"California",
        "zip_code":"93001",
        "country":"United States",
        "full_address_with_comma":"2575 Vista Del Mar, Ventura, California, 93001, United States",
        "phone":"(270) 901-2913",
        "fax":null,
        "currency":{"name":"United States of America, Dollars","symbol":"$"},
        "custom_field_name":null,
        "custom_field_value":null
        "created_at":"2010-07-18T00:08:10Z",
        "updated_at":"2010-07-18T00:08:10Z",
      }
}]

我创建了一个相应的Java Pojo类

I have created a corresponding Java Pojo class

public class Client {
@JsonProperty("id")
private String id;
@JsonProperty("name")
private String name;
@JsonProperty("currency")
private String currency;
@JsonProperty("address")
private String address;
@JsonProperty("city")
private String city;
@JsonProperty("province")
private String province;
@JsonProperty("zip_code")
private String zip_code;
@JsonProperty("country")
private String country;
@JsonProperty("full_address_with_comma")
private String full_address_with_comma;
@JsonProperty("phone")
private String phone;
@JsonProperty("fax")
private String fax;
@JsonProperty("custom_field_name")
private String custom_field_name;
@JsonProperty("custom_field_value")
private String custom_field_value;
@JsonProperty("created_at")
private String created_at;
@JsonProperty("updated_at")
private String updated_at;

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getCurrency() {
    return currency;
}
public void setCurrency(String currency) {
    this.currency = currency;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getProvince() {
    return province;
}
public void setProvince(String province) {
    this.province = province;
}
public String getZip_code() {
    return zip_code;
}
public void setZip_code(String zip_code) {
    this.zip_code = zip_code;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getFull_address_with_comma() {
    return full_address_with_comma;
}
public void setFull_address_with_comma(String full_address_with_comma) {
    this.full_address_with_comma = full_address_with_comma;
}
public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}
public String getFax() {
    return fax;
}
public void setFax(String fax) {
    this.fax = fax;
}
public String getCustom_field_name() {
    return custom_field_name;
}
public void setCustom_field_name(String custom_field_name) {
    this.custom_field_name = custom_field_name;
}
public String getCustom_field_value() {
    return custom_field_value;
}
public void setCustom_field_value(String custom_field_value) {
    this.custom_field_value = custom_field_value;
}
public String getCreated_at() {
    return created_at;
}
public void setCreated_at(String created_at) {
    this.created_at = created_at;
}
public String getUpdated_at() {
    return updated_at;
}
public void setUpdated_at(String updated_at) {
    this.updated_at = updated_at;
}

}

我正在使用RestTemplate,但我有一组具有空属性值的客户端。

I'm using RestTemplate, but I have got an array of clients with empty attribute values.

Client[] clients= restTemplate.getForObject(requestUrl, Client[].class);


推荐答案

您的大部分POJO字段都是<$ c类型$ c> String 但是你的JSON的值没有双引号()。您的JSON应如下有效:

Most of your POJO fields are of type String but your JSON has values without double quotes (""). Your JSON should be as follows to be valid:

[
    {
        "client": {
            "id": "6364",
            "name": "7Seven7 Insurance Inc",
            "email": "donna@7seven7ins.com",
            "currency": {
                "name": "United States of America, Dollars",
                "symbol": "$"
            },
            "address": "941 Elm Ave. #5 ",
            "city": "Long Beach",
            "province": "CA",
            "zip_code": "90813",
            "country": "United States",
            "full_address_with_comma": "941 Elm Ave. #5, Long Beach, CA, 90813, United States",
            "phone": "562-556-4035",
            "fax": "562-381-7500",
            "custom_field_name": "listed",
            "custom_field_value": "false",
            "created_at": "2010-07-18T00:08:10Z",
            "updated_at": "2010-07-21T11:04:58Z"
        }
    },
    {
        "client": {
            "id": "6365",
            "name": "Affinity",
            "email": "CGregory@affinitygroup.com",
            "address": "2575 Vista Del Mar ",
            "city": "Ventura",
            "province": "California",
            "zip_code": "93001",
            "country": "United States",
            "full_address_with_comma": "2575 Vista Del Mar, Ventura, California, 93001, United States",
            "phone": "(270) 901-2913",
            "fax": "null",
            "currency": {
                "name": "United States of America, Dollars",
                "symbol": "$"
            },
            "custom_field_name": "null",
            "custom_field_value": "null",
            "created_at": "2010-07-18T00:08:10Z",
            "updated_at": "2010-07-18T00:08:10Z"
        }
    }
]

另外,你r JSON有电子邮件字段,但您的客户 POJO没有电子邮件字段,并且您在POJO中声明的货币字段不是字符串,它是一个包含两个字段的对象,所以你的客户 POJO应该是:

Also, your JSON has an email field but your Client POJO has not email field, and your declared currency field in the POJO is not a String, it's an object with two fields, so your Client POJO should be:

public class Client {

    private String id;
    private String name;
    private String email;
    private Currency currency;
    private String address;
    private String city;
    private String province;
    private String zip_code;
    private String country;
    private String full_address_with_comma;
    private String phone;
    private String fax;
    private String custom_field_name;
    private String custom_field_value;
    private String created_at;
    private String updated_at;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Currency getCurrency() {
        return currency;
    }

    public void setCurrency(Currency currency) {
        this.currency = currency;
    }

    public String getAddress() {
        return address;
    }

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

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getZip_code() {
        return zip_code;
    }

    public void setZip_code(String zip_code) {
        this.zip_code = zip_code;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getFull_address_with_comma() {
        return full_address_with_comma;
    }

    public void setFull_address_with_comma(String full_address_with_comma) {
        this.full_address_with_comma = full_address_with_comma;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public String getCustom_field_name() {
        return custom_field_name;
    }

    public void setCustom_field_name(String custom_field_name) {
        this.custom_field_name = custom_field_name;
    }

    public String getCustom_field_value() {
        return custom_field_value;
    }

    public void setCustom_field_value(String custom_field_value) {
        this.custom_field_value = custom_field_value;
    }

    public String getCreated_at() {
        return created_at;
    }

    public void setCreated_at(String created_at) {
        this.created_at = created_at;
    }

    public String getUpdated_at() {
        return updated_at;
    }

    public void setUpdated_at(String updated_at) {
        this.updated_at = updated_at;
    }

    public String getEmail() {
        return email;
    }

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

}

货币对象:

public class Currency {

    private String name;
    private String symbol;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

}

另一方面,你是尝试反序列化 Client 对象的数组,但您的JSON是一个对象数组,其中每个对象包含一个 Client 对象,所以你需要包装它:

On the other hand, you are trying to deserialize an array of Client objects, but your JSON is an array of objects where each object contains a Client object, so you need to wrap it:

public class Cliente {

    private Client client;

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

}

然后你可以反序列化你的JSON使用 restTemplate 或使用 ObjectMapper

使用 restTemplate

and then you could deserialize your JSON with the restTemplate or with an ObjectMapper.
With a restTemplate:

Cliente[] clients= restTemplate.getForObject(requestUrl, Cliente[].class);

使用 ObjectMapper (杰克逊 genericMessageConverter 完全相同,它使用 ObjectMapper ,如下所示:

With an ObjectMapper (Jackson's genericMessageConverter does exactly the same, it uses an ObjectMapper as follows):

Cliente[] clientes= mapper.readValue(jsonStr, Cliente[].class);

另一件事是,你不需要 @JsonProperty 注释。

Another thing is, you do not need the @JsonProperty annotations in your POJO if your JSON fields have the same name in the JSON and in the POJO.

这篇关于Json Array到Pojo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆