栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Json Array到Pojo

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Json Array到Pojo

您的大多数POJO字段都是类型,

String
但JSON的值中没有双引号(
""
)。您的JSON应该如下所示才能有效:

[    {        "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_pre": "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_pre": "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"        }    }]

另外,您的JSON有一个

email
字段,但您的
Client
POJO没有
email
字段,并且您
currency
在POJO中声明的字段不是
String
,它是一个具有两个字段的对象,因此您的
Client
POJO应该为:

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_pre;    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_pre() {        return zip_pre;    }    public void setZip_pre(String zip_pre) {        this.zip_pre = zip_pre;    }    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;    }}

有一个新

Currency
对象:

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
对象,因此需要包装它:

public class Cliente {    private Client client;    public Client getClient() {        return client;    }    public void setClient(Client client) {        this.client = client;    }}

然后可以使用

restTemplate
或使用反序列化JSON
ObjectMapper

带有
restTemplate

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

使用

ObjectMapper
(杰克逊的方法
genericMessageConverter
完全相同,它使用
ObjectMapper
如下):

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

另一件事是,

@JsonProperty
如果JSON字段在JSON和POJO中具有相同的名称,则在POJO中不需要注释。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377097.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号