上午有一個JSON文件,如下轉換JSON來Java對象使用保留變量
{
"boost_id": "75149",
"content_id": "627680",
"headline": "19 Rare Historical Photos That Will Leave You Breathless ",
"target_url": "http://stars.americancolumn.com/2016/02/21/historical-photos/?full=1",
"return": "district"
}
我需要JSON字符串轉換成Java對象。由於「返回」被稱爲Java保留關鍵字,因此無法與返回變量形成Dto。
是否有任何其他方式使用reserve變量並將上面的JSON轉換爲JAVA對象。
下面是我的DTO結構,
public class RevcontentReportResponse {
private String boost_id;
private String content_id;
private String headline;
private String target_url;
// private String return;
public String getBoost_id() {
return boost_id;
}
public void setBoost_id(String boost_id) {
this.boost_id = boost_id;
}
public String getContent_id() {
return content_id;
}
public void setContent_id(String content_id) {
this.content_id = content_id;
}
public String getHeadline() {
return headline;
}
public void setHeadline(String headline) {
this.headline = headline;
}
public String getTarget_url() {
return target_url;
}
public void setTarget_url(String target_url) {
this.target_url = target_url;
}
}
主要方法:
ObjectMapper mapper = new ObjectMapper();
File json = new File("historic.json");
RevcontentReportResponse cricketer = mapper.readValue(json, RevcontentReportResponse.class);
System.out.println("Java object created from JSON String :");
System.out.println(cricketer);
像一個魅力工作 –