我有一个如下的 JSON 文件
{
"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 对象。由于“return”被称为 Java 保留关键字,因此无法形成带有返回变量的 Dto。
有没有其他方法可以使用保留变量并将上述 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);