Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将JSON字符串转换为多个POJO';产生了什么?_Java_Json_Jackson - Fatal编程技术网

Java 如何将JSON字符串转换为多个POJO';产生了什么?

Java 如何将JSON字符串转换为多个POJO';产生了什么?,java,json,jackson,Java,Json,Jackson,我有下面的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; imp

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

}
我的输出如下:

信息[stdout](http localhost/127.0.0.1:8080-214) 杰森波乔。EventPayload@40310afd


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

看起来不错,您只需要在其中获取成员变量:

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

可能将
mapper.readValue(message,EventPayload.class)
更改为
mapper.readValue(message,EventPojo.class)
您不清楚要实现什么。您希望打印语句输出什么?谢谢..这就是我要找的..我认为System.out.println(eventpayload);将打印整个结果:)
    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();
    }
System.out.println(eventpayload.getCommentText());