解析JSON响应会给出null值

时间:2014-09-12 09:14:40

标签: java json jackson

我的代码解析JSON

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);        
JsonNode rootNode = mapper.readTree(response.toString());
JsonNode entriesNode = rootNode.path("messages");
StoreMessageList objects = mapper.readValue(rootNode.toString(), new TypeReference<StoreMessageLis(){});    
Log.d("JacksonParser", objects.toString());

以下是我的POJO课程:

@JsonIgnoreProperties(ignoreUnknown = true)
public class StoreMessageList {

    private String location;

    @JsonProperty("messages")
    List<MessageList> messages;

    public StoreMessageList() {
    }

    public StoreMessageList(String location, List<MessageList> messages) {

        this.location = location;
        this.messages = messages;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public List<MessageList> getMessages() {
        return this.messages;
    }

    public void setMessages(List<MessageList> messages) {
        this.messages = messages;
    }
    @Override
    public String toString() {
        return "StoreMessageList:{location: " + location + ", messages: " + messages + "}";
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class MessageList implements Serializable{

        @JsonProperty("Text")
        private String Text;

        @JsonProperty("DetailPicture")
        private String DetailPicture;

        @JsonProperty("Title")
        private String Title;

        @JsonProperty("ListPicture")
        private String ListPicture;

        public MessageList() {
        }

        public MessageList(String Text, String DetailPicture, String Title, String ListPicture) {
            this.Text = Text;
            this.DetailPicture = DetailPicture;
            this.Title = Title;
            this.ListPicture = ListPicture;

        }

        public String getText() {     
            return this.Text;
        }

        public void setText(String text) {
            this.Text = text;
        }

        public String getDetailPicture() {
            return this.DetailPicture;
        }

        public void setDetailPicture(String detailPicture) {
            this.DetailPicture = detailPicture;
        }

        public String getTitle() {
            return this.Title;
        }

        public void setTitle(String title) {
            this.Title = title;
        }

        public String getListPicture() {
            return this.ListPicture;
        }

        public void setListPicture(String listPicture) {
            this.ListPicture = listPicture;
        }

        @Override
        public String toString() {
            return "Messages:{Text: " + Text + ", DetailPicture: " + DetailPicture + ", ListPicture: " + ListPicture +
                        ", Title: " + Title + "}";
        }

    }
}

请帮助我获取值而不是null值。 JSON响应:

{
    "_links": {
        "self": {
            "href": "http://10.1.1.228:8880/messages/1"
        }
    },
    "location": "ABC Store",
    "messages": [
        {
            "DetailPicture": "/images/2_detailpic.png",
            "Text": "This is a dummy text. This is a dummy text. This is a dummy text. This is a dummy text. This is a dummy text. This is a dummy text. This is a dummy text. This is a dummy text. This is a dummy text. ",
            "ListPicture": "/images/2_listpic.png",
            "Title": "Test Title"
        },
        {
            "Text": "dummy dummy dummy",
            "ListPicture": "/images/3_listpic.png",
            "Title": "Test News"
        },
        {
            "Text": "dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text dummy text du",
            "ListPicture": "/images/4_listpic.png",
            "Title": "News 3"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

您的MessageList课程是内部课程。没有外部类的实例,不能存在static的内部类。在我看来,你必须将这个类标记为static,因为下面的代码应该正常工作:

ObjectMapper deserializerMapper = new ObjectMapper();
StoreMessageList root = deserializerMapper.readValue(json, StoreMessageList.class);
System.out.println(root);