我有以下JSON
{"event_type": "[new,update,delete,close]","event_payload": [{"comment_id":
123,"comment_text": "","comment_type": "DIDWELL"}],"event_retrospective_id":
500,"event_error": ""}
生成的Pojo类如下:
package jsonpojo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"event_type",
"event_payload",
"event_retrospective_id ",
"event_error"
})
public class EventPojo {
@JsonProperty("event_type")
private String eventType;
@JsonProperty("event_payload")
private List<EventPayload> eventPayload = null;
@JsonProperty("event_retrospective_id ")
private Integer eventRetrospectiveId;
@JsonProperty("event_error")
private String eventError;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("event_type")
public String getEventType() {
return eventType;
}
@JsonProperty("event_type")
public void setEventType(String eventType) {
this.eventType = eventType;
}
@JsonProperty("event_payload")
public List<EventPayload> getEventPayload() {
return eventPayload;
}
@JsonProperty("event_payload")
public void setEventPayload(List<EventPayload> eventPayload) {
this.eventPayload = eventPayload;
}
@JsonProperty("event_retrospective_id ")
public Integer getEventRetrospectiveId() {
return eventRetrospectiveId;
}
@JsonProperty("event_retrospective_id ")
public void setEventRetrospectiveId(Integer eventRetrospectiveId) {
this.eventRetrospectiveId = eventRetrospectiveId;
}
@JsonProperty("event_error")
public String getEventError() {
return eventError;
}
@JsonProperty("event_error")
public void setEventError(String eventError) {
this.eventError = eventError;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
EventPayload.java
package jsonpojo;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"comment_id",
"comment_text",
"comment_type"
})
public class EventPayload {
@JsonProperty("comment_id")
private Integer commentId;
@JsonProperty("comment_text")
private String commentText;
@JsonProperty("comment_type")
private String commentType;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("comment_id")
public Integer getCommentId() {
return commentId;
}
@JsonProperty("comment_id")
public void setCommentId(Integer commentId) {
this.commentId = commentId;
}
@JsonProperty("comment_text")
public String getCommentText() {
return commentText;
}
@JsonProperty("comment_text")
public void setCommentText(String commentText) {
this.commentText = commentText;
}
@JsonProperty("comment_type")
public String getCommentType() {
return commentType;
}
@JsonProperty("comment_type")
public void setCommentType(String commentType) {
this.commentType = commentType;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
我的主要课程::
ObjectMapper mapper=new ObjectMapper();
try {
EventPayload eventpayload = mapper.readValue(message, EventPayload.class);
System.out.println(eventpayload);
} catch (JsonParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (JsonMappingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
我的输出如下:
INFO [stdout](http-localhost / 127.0.0.1:8080-214) jsonpojo.EventPayload@40310afd
我需要使用POJO的.Kindly帮助映射JSON。
答案 0 :(得分:0)
看起来不错,你只需要在其中获取成员变量:
System.out.println(eventpayload.getCommentText());