使用 resttemplate 解析 JSON 数组
Parse JSON array with resttemplate
我想我有一个很常见的问题,但我找不到解决方案:(
我正在使用 spring 和 restTemplate 来恢复 JSON 对象,如下所示:
ResponseEntity<Download_urls> result= restTemplate.exchange(URL, HttpMethod.GET, entity, Download_urls.class);
其中 "Download_urls " class 里面有一个 JSON 数组:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Download_urls {
private Video[] video;
}
和Video.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;
}
}
显然视频[] 无法映射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 属性并且有效:
public Video[] video;
您的 Java class 名称及其属性应遵循 Java 命名约定。那么你的代码就更具可读性和更好。要将 JSON 字段名称来回转换,您可以使用命名策略,例如:LowerCaseWithUnderscoresStrategy
、PascalCaseStrategy
等
这是我认为 class 应该是这样的:
Video.java - 和你的一样。
DownloadUrls.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;
}
}
迈克尔
我想我有一个很常见的问题,但我找不到解决方案:(
我正在使用 spring 和 restTemplate 来恢复 JSON 对象,如下所示:
ResponseEntity<Download_urls> result= restTemplate.exchange(URL, HttpMethod.GET, entity, Download_urls.class);
其中 "Download_urls " class 里面有一个 JSON 数组:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Download_urls {
private Video[] video;
}
和Video.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;
}
}
显然视频[] 无法映射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 属性并且有效:
public Video[] video;
您的 Java class 名称及其属性应遵循 Java 命名约定。那么你的代码就更具可读性和更好。要将 JSON 字段名称来回转换,您可以使用命名策略,例如:LowerCaseWithUnderscoresStrategy
、PascalCaseStrategy
等
这是我认为 class 应该是这样的:
Video.java - 和你的一样。
DownloadUrls.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;
}
}
迈克尔