Arrays 使用resttemplate解析JSON数组
我想我有一个非常常见的问题,但我找不到解决办法:( 我使用spring和restTemplate恢复JSON对象,如下所示:Arrays 使用resttemplate解析JSON数组,arrays,json,jackson,mapping,resttemplate,Arrays,Json,Jackson,Mapping,Resttemplate,我想我有一个非常常见的问题,但我找不到解决办法:( 我使用spring和restTemplate恢复JSON对象,如下所示: ResponseEntity<Download_urls> result= restTemplate.exchange(URL, HttpMethod.GET, entity, Download_urls.class); 还有视频课 @JsonIgnoreProperties(ignoreUnknown = true) public class Video
ResponseEntity<Download_urls> result= restTemplate.exchange(URL, HttpMethod.GET, entity, Download_urls.class);
还有视频课
@JsonIgnoreProperties(ignoreUnknown = true)
public class Video {
private String type;
private String label;
private String file;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
}
显然,Video[]无法映射JSON数组。有什么帮助吗
谢谢
更新:
示例JSON有效负载:
{
"id": 737132,
"asset": {
"_class": "asset",
"id": 538362,
"download_urls": {
"Video": [{
"type": "video/mp4",
"label": "360"
}, {
"type": "video/mp4",
"label": "720"
}]
}
}
}
解决了
这是我的:
private Video[] video;
已尝试使用公共属性,但效果良好:
public Video[] video;
解决了
这是我的:
private Video[] video;
已尝试使用公共属性,但效果良好:
public Video[] video;
您的Java类名及其属性应该遵循Java命名约定。这样您的代码就更可读、更好。要来回转换JSON字段名,您可以使用命名策略,例如:
lowercasewirethCoresStrategy
,PascalCaseStrategy
,等等
以下是I thing类的外观:
java-与您的相同
下载URL.java:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.PascalCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
@JsonIgnoreProperties(ignoreUnknown = true)
// PascalCaseStrategy is used here because of "Video" JSON field. I would expect
// it to be called "video".
@JsonNaming(PascalCaseStrategy.class)
public class DownloadUrls {
private Video[] video;
public Video[] getVideo() {
return video;
}
public void setVideo(Video[] video) {
this.video = video;
}
}
Asset.java:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
@JsonIgnoreProperties(ignoreUnknown = true)
// LowerCaseWithUnderscoresStrategy is common strategy when used with JSON, but
// in this case it is used because of "download_url" JSON field only.
@JsonNaming(LowerCaseWithUnderscoresStrategy.class)
public class Asset {
int id;
DownloadUrls downloadUrls;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public DownloadUrls getDownloadUrls() {
return downloadUrls;
}
public void setDownloadUrls(DownloadUrls downloadUrls) {
this.downloadUrls = downloadUrls;
}
}
和外部类型只是为了完整性:
public class OuterType {
int id;
Asset asset;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Asset getAsset() {
return asset;
}
public void setAsset(Asset asset) {
this.asset = asset;
}
}
Michal您的Java类名及其属性应该遵循Java命名约定。这样,您的代码就更可读、更美观。要来回转换JSON字段名,您可以使用命名策略,例如:
lowercasewiredCoresStrategy
,PascalCaseStrategy
,等等
以下是I thing类的外观:
java-与您的相同
下载URL.java:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.PascalCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
@JsonIgnoreProperties(ignoreUnknown = true)
// PascalCaseStrategy is used here because of "Video" JSON field. I would expect
// it to be called "video".
@JsonNaming(PascalCaseStrategy.class)
public class DownloadUrls {
private Video[] video;
public Video[] getVideo() {
return video;
}
public void setVideo(Video[] video) {
this.video = video;
}
}
Asset.java:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
@JsonIgnoreProperties(ignoreUnknown = true)
// LowerCaseWithUnderscoresStrategy is common strategy when used with JSON, but
// in this case it is used because of "download_url" JSON field only.
@JsonNaming(LowerCaseWithUnderscoresStrategy.class)
public class Asset {
int id;
DownloadUrls downloadUrls;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public DownloadUrls getDownloadUrls() {
return downloadUrls;
}
public void setDownloadUrls(DownloadUrls downloadUrls) {
this.downloadUrls = downloadUrls;
}
}
和外部类型只是为了完整性:
public class OuterType {
int id;
Asset asset;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Asset getAsset() {
return asset;
}
public void setAsset(Asset asset) {
this.asset = asset;
}
}
Michal你能发布一个JSON有效载荷的例子吗?当然@MichalFoksa!!是这样的:{“id”:737132,“资产”:{“资产”,“id”:538362,“下载URL”:{“视频”:[{“类型”:“视频/mp4”,“标签”:“360”},{“类型”:“video/mp4”、“label”:“720”}]}}}}我无法从下载中检索视频JSON数组。\u url谢谢!您定义了资产类及其外部类型(带有id和资产)吗?是的,在我尝试调试下载\u url类时,在视频类变为null之前一切都很顺利。删除
@JsonIgnoreProperties(ignoreUnknown=true)
来自下载url类。这会给你一条有用的消息,告诉你哪里出了问题。你能发布一个JSON负载的例子吗?当然@MichalFoksa!!是这样的:{“id”:737132,“asset”:{“class”:“asset”,“id”:538362,“下载url”:{“Video”:[{“type”:视频/mp4,“标签”:“360”},{“类型”:“视频/mp4”,“标签”:“720”}]}}}}我无法从下载中检索视频JSON数组谢谢!您是否定义了资产类及其外部类型(带id和资产)是的,当我试着调试下载url类时,直到视频类变为null,一切都很顺利。从下载url类中删除@JsonIgnoreProperties(ignoreUnknown=true)
。这会给你一些有用的信息,告诉你哪里出了问题。