我正在尝试解析一个JSON文件,它是通过API到pojo获得的。在网上搜索之后,我看到布恩在休息,但我不知道怎么做。根据这个文章,它应该能工作,但是..。
在我的代码中,HTTP.getJSON()方法需要一个映射作为参数,而我不知道这个映射到底是什么。
任何一个天才都可以给出一个实惠的例子?
public class ViewTimeline{
public void view() {
ObjectMapper mapper = JsonFactory.create();
List<String> read = IO.readLines("https://corona-api.com/timeline");
Map<String, ?> headers = null ;
List<Timeline> timelineList = mapper.readValue(HTTP.getJSON("https://corona-api.com/timeline", headers), List.class, Timeline.class);
}
}TimeLine.java
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"updated_at",
"date",
"deaths",
"confirmed",
"recovered",
"active",
"new_confirmed",
"new_recovered",
"new_deaths",
"is_in_progress"
})
public class Timeline {
@JsonProperty("updated_at")
private String updatedAt;
@JsonProperty("date")
private String date;
@JsonProperty("deaths")
private Integer deaths;
@JsonProperty("confirmed")
private Integer confirmed;
@JsonProperty("recovered")
private Integer recovered;
@JsonProperty("active")
private Integer active;
@JsonProperty("new_confirmed")
private Integer newConfirmed;
@JsonProperty("new_recovered")
private Integer newRecovered;
@JsonProperty("new_deaths")
private Integer newDeaths;
@JsonProperty("is_in_progress")
private Boolean isInProgress;
@JsonProperty("updated_at")
public String getUpdatedAt() {
return updatedAt;
}
@JsonProperty("updated_at")
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
@JsonProperty("date")
public String getDate() {
return date;
}
@JsonProperty("date")
public void setDate(String date) {
this.date = date;
}
@JsonProperty("deaths")
public Integer getDeaths() {
return deaths;
}
@JsonProperty("deaths")
public void setDeaths(Integer deaths) {
this.deaths = deaths;
}
@JsonProperty("confirmed")
public Integer getConfirmed() {
return confirmed;
}
@JsonProperty("confirmed")
public void setConfirmed(Integer confirmed) {
this.confirmed = confirmed;
}
@JsonProperty("recovered")
public Integer getRecovered() {
return recovered;
}
@JsonProperty("recovered")
public void setRecovered(Integer recovered) {
this.recovered = recovered;
}
@JsonProperty("active")
public Integer getActive() {
return active;
}
@JsonProperty("active")
public void setActive(Integer active) {
this.active = active;
}
@JsonProperty("new_confirmed")
public Integer getNewConfirmed() {
return newConfirmed;
}
@JsonProperty("new_confirmed")
public void setNewConfirmed(Integer newConfirmed) {
this.newConfirmed = newConfirmed;
}
@JsonProperty("new_recovered")
public Integer getNewRecovered() {
return newRecovered;
}
@JsonProperty("new_recovered")
public void setNewRecovered(Integer newRecovered) {
this.newRecovered = newRecovered;
}
@JsonProperty("new_deaths")
public Integer getNewDeaths() {
return newDeaths;
}
@JsonProperty("new_deaths")
public void setNewDeaths(Integer newDeaths) {
this.newDeaths = newDeaths;
}
@JsonProperty("is_in_progress")
public Boolean getIsInProgress() {
return isInProgress;
}
@JsonProperty("is_in_progress")
public void setIsInProgress(Boolean isInProgress) {
this.isInProgress = isInProgress;
}
}发布于 2020-06-07 11:38:32
为了将json解析为对象,我使用了Jackson。我还看到您在Timeline的映射中使用了Timeline。
杰克逊核心:https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core/2.11.0
杰克逊数据库:https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.11.0
杰克逊注释:https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations/2.11.0
我就是这样处理的:
public static void main(String[] args) throws JsonProcessingException {
//my method to read content from website.
//using apache http
String jsonApi = getApi();
ObjectMapper objectMapper = new ObjectMapper();
//todo JsonProcessingException
JsonNode data = objectMapper.readTree(jsonApi);
//get data field from data, which is an array
//todo This can throws error if data field is missing
JsonNode dataArray = data.get("data");
List<Timeline> timelineList = new ArrayList<>();
if(dataArray.isArray()){
for(JsonNode line : dataArray){
//todo this can throws errors. need to handle it.
Timeline timeline = objectMapper.readValue(line.toString(), Timeline.class);
timelineList.add(timeline);
}
}else{
System.out.println("JsonApi is not array: '" + jsonApi + "'");
}
System.out.println("Size: " + timelineList.size());
for(Timeline timeline : timelineList){
System.out.println(timeline.getConfirmed());
}
}在这段代码中,您应该处理异常。我用评论来标记它们。
https://stackoverflow.com/questions/62244322
复制相似问题