我试图解封以下Json数组响应到RESTTemplate中的POJO集合。
[{
"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类
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,但我有一个带有空属性值的客户端数组。
Client[] clients= restTemplate.getForObject(requestUrl, Client[].class);
大多数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_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"
}
}
]
此外,您的JSON有一个email
字段,但您的client
POJO没有email
字段,并且您在POJO中声明的currency
字段不是一个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_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;
}
}
使用新的currence
对象:
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
或ObjectMapper
反序列化JSON。
使用RestTemplate
:
Cliente[] clients= restTemplate.getForObject(requestUrl, Cliente[].class);
Cliente[] clientes= mapper.readValue(jsonStr, Cliente[].class);
另一个问题是,如果您的JSON字段在JSON和POJO中具有相同的名称,您就不需要POJO中的@JSONProperty
注释。
问题内容: 我在处理JavaScript中的JSON数据时遇到问题,特别是在将数据用作数组以及访问和迭代各个值方面。JSON文件的结构如下: 我希望能够将其转换为如下所示的JavaScript数组,以便可以迭代它并按顺序提取每个位置的值: 有人对如何实现这一目标有任何建议吗?我已经尝试了以下方法,但是它在JSON中选择了“类型”,而不仅仅是值: 任何帮助,建议,建议或更正将不胜感激。 问题答案:
问题内容: 是否可以通过json_encode对这个php数组进行JSON转换?因为这个php数组被调用,当我做.. 在JavaScript中,它给了我错误。我想知道JSON可以/不能做什么类型的php数组是否有限制。 问题答案: 首先,您需要在安装了PHP的Apache服务器上的一个PHP文件。制作如下文件: 然后是您的JavaScript(在此示例中,我使用jQuery): 这应该是开始将PH
问题内容: 我如何像这样转换JSON 数组 到带有json.net或系统类的C#词典。 json.net可以直接序列化为字典, 但 前提 是您向其提供一个对象 而不是数组。 问题答案: 也许转换为KeyValuePairs数组会有所帮助
问题内容: 如何将JavaScript关联数组转换为JSON? 我尝试了以下方法: 问题答案: 数组应仅包含带有数字键的条目(数组也是对象,但您实际上不应混用)。 如果将数组转换为JSON,则该过程将仅考虑数值属性。其他属性只是被忽略,这就是为什么您得到一个空数组的原因。如果您看一下数组的,也许这更加明显: 通常被称为“关联数组”的实际上只是JS中的一个对象: 可以通过数组符号或点符号(如果键不是
问题内容: 我需要将PHP数组转换为json,但我没有得到我期望的结果。我希望它是一个可以轻松使用数字索引导航的对象。这是一个示例代码: 这就是我得到的 但是我想得到一个对象而不是数组: 谢谢 :) 问题答案: 你想。 顾名思义,该标志将json输出强制为一个对象,即使通常将其表示为数组也是如此。 您还可以删除一些更简洁的代码:
问题内容: 我需要解析一个看起来像这样的json文件: 我想将这些X坐标和Y坐标放入JavaObject Click中,该类如下所示: 我看过gson是因为他们说这很容易,但是我不知道如何从文件中做到这一点。 问题答案: 假设您的json字符串数据存储在名为的变量中: