簡體   English   中英

如何將 JSON 字符串轉換為生成多個 POJO 的 POJO?

[英]How to convert JSON string to POJO where multiple POJO's are generated?

我有以下 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);
}

}

事件有效載荷.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();
    }

我的輸出如下:

信息 [標准輸出] (http-localhost/127.0.0.1:8080-214) jsonpojo.EventPayload@40310afd

我需要用 POJO's.Kindly 幫助映射的 JSON。

看起來不錯,只需要獲取其中的成員變量即可:

System.out.println(eventpayload.getCommentText());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM