【问题标题】:How to convert JSON string to POJO where multiple POJO's are generated?如何将 JSON 字符串转换为生成多个 POJO 的 POJO?
【发布时间】:2017-02-06 09:09:32
【问题描述】:

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

我的输出如下:

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

我需要使用 POJO 映射的 JSON。请帮助。

【问题讨论】:

  • 也许把mapper.readValue(message, EventPayload.class)改成mapper.readValue(message, EventPojo.class)
  • 不清楚您要达到的目标。您希望您的打印语句输出什么?

标签: java json jackson


【解决方案1】:

看起来不错,只需要获取里面的成员变量即可:

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

【讨论】:

  • 谢谢..这就是我要找的..我想 System.out.println(eventpayload);将打印整个结果::)
猜你喜欢
  • 1970-01-01
  • 2020-04-27
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多