json字符串到java对象映射失败了杰克逊

时间:2017-08-15 11:32:34

标签: java json jackson deserialization pojo

我正在尝试将我的json映射到与杰克逊的POJO类,但我收到空值。 如果我删除@JsonIgnoreProperties注释,则会抛出无法识别的属性

的异常

JSON:

{
   "TileLevel":[

   ],
   "SystemLevel":[
      {
         "Title":"Test System Level Alert",
         "Description":"<div class=\"ExternalClassA5AAC8F74C874A2D9DB8337EB6685B2A\"><p>​Test System Level Alert​<br></p><p>check the url http&#58;//www.google.com<br></p><p><span style=\"font-size&#58;32px;\">formatting tested</span><br></p></div>",
         "AnnouncementType":"System Level",
         "ActiveF":null,
         "TileName":null
      }
   ],
   "Announcements":[
      {
         "Title":"Test Announcement",
         "Description":"<div class=\"ExternalClass16980D07D2FB4AB58A2E97C16B7626D3\"><p>​Testing.....&#160;<img src=\"/sites/salesbk-CN/SiteCollectionImages/Salesbook%20Announcements/AllItems/00007-confiz-test-doc7.png\" alt=\"00007-confiz-test-doc7.png\" style=\"margin&#58;5px;\" /><br></p></div>",
         "AnnouncementType":"Announcement",
         "ActiveF":null,
         "TileName":null
      }
   ]
}

我将这个json作为字符串,然后尝试将此字符串转换为java对象。

以下是我的POJO课程:

public class SPOAnnouncement {

    @JsonIgnoreProperties
    private Map<String, List<List<SPOAnnouncements_Properties>>> TileLevel;
    private Map<String, List<List<SPOAnnouncements_Properties>>> SystemLevel;
    private Map<String, List<List<SPOAnnouncements_Properties>>> Announcements;

    public Map<String, List<List<SPOAnnouncements_Properties>>> getTileLevel() {
        return TileLevel;
    }

    public void setTileLevel(Map<String, List<List<SPOAnnouncements_Properties>>> tileLevel) {
        TileLevel = tileLevel;
    }

    public Map<String, List<List<SPOAnnouncements_Properties>>> getSystemLevel() {
        return SystemLevel;
    }

    public void setSystemLevel(Map<String, List<List<SPOAnnouncements_Properties>>> systemLevel) {
        SystemLevel = systemLevel;
    }

    public Map<String, List<List<SPOAnnouncements_Properties>>> getAnnouncements() {
        return Announcements;
    }

    public void setAnnouncements(Map<String, List<List<SPOAnnouncements_Properties>>> announcements) {
        Announcements = announcements;
    }
}

public class SPOAnnouncements_Properties {


    private String Title;
    private String Description;
    private String AnnouncementType;
    private String ActiveF;
    private String TileName;

    public String getTitle() {
        return Title;
    }

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

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getAnnouncementType() {
        return AnnouncementType;
    }

    public void setAnnouncementType(String announcementType) {
        AnnouncementType = announcementType;
    }

    public String getActiveF() {
        return ActiveF;
    }

    public void setActiveF(String activeF) {
        ActiveF = activeF;
    }

    public String getTileName() {
        return TileName;
    }

    public void setTileName(String tileName) {
        TileName = tileName;
    }

}

我的测试方法,用于验证结果是否已映射到json对象:

@Test
public void convertJsonToObject() throws IOException {

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    SPOAnnouncement spoAnnouncement = mapper.readValue(response, SPOAnnouncement.class);
    System.out.print(spoAnnouncement);
} 

2 个答案:

答案 0 :(得分:1)

您的POJO结构无效。查看http://www.jsonschema2pojo.org - 一个从JSON生成Java POJO的在线工具。

您的类应该看起来或多或少如下,但您需要将ActiveFTileName字段替换为优先类型。

-----------------------------------com.example.Announcement.java-----------------------------------

package com.example;

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({
        "Title",
        "Description",
        "AnnouncementType",
        "ActiveF",
        "TileName"
})
public class Announcement {

    @JsonProperty("Title")
    private String title;
    @JsonProperty("Description")
    private String description;
    @JsonProperty("AnnouncementType")
    private String announcementType;
    @JsonProperty("ActiveF")
    private Object activeF;
    @JsonProperty("TileName")
    private Object tileName;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("Title")
    public String getTitle() {
        return title;
    }

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

    @JsonProperty("Description")
    public String getDescription() {
        return description;
    }

    @JsonProperty("Description")
    public void setDescription(String description) {
        this.description = description;
    }

    @JsonProperty("AnnouncementType")
    public String getAnnouncementType() {
        return announcementType;
    }

    @JsonProperty("AnnouncementType")
    public void setAnnouncementType(String announcementType) {
        this.announcementType = announcementType;
    }

    @JsonProperty("ActiveF")
    public Object getActiveF() {
        return activeF;
    }

    @JsonProperty("ActiveF")
    public void setActiveF(Object activeF) {
        this.activeF = activeF;
    }

    @JsonProperty("TileName")
    public Object getTileName() {
        return tileName;
    }

    @JsonProperty("TileName")
    public void setTileName(Object tileName) {
        this.tileName = tileName;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}
-----------------------------------com.example.SPOAnnouncement.java-----------------------------------

package com.example;

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({
        "TileLevel",
        "SystemLevel",
        "Announcements"
})
public class SPOAnnouncement {

    @JsonProperty("TileLevel")
    private List<Object> tileLevel = null;
    @JsonProperty("SystemLevel")
    private List<SystemLevel> systemLevel = null;
    @JsonProperty("Announcements")
    private List<Announcement> announcements = null;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("TileLevel")
    public List<Object> getTileLevel() {
        return tileLevel;
    }

    @JsonProperty("TileLevel")
    public void setTileLevel(List<Object> tileLevel) {
        this.tileLevel = tileLevel;
    }

    @JsonProperty("SystemLevel")
    public List<SystemLevel> getSystemLevel() {
        return systemLevel;
    }

    @JsonProperty("SystemLevel")
    public void setSystemLevel(List<SystemLevel> systemLevel) {
        this.systemLevel = systemLevel;
    }

    @JsonProperty("Announcements")
    public List<Announcement> getAnnouncements() {
        return announcements;
    }

    @JsonProperty("Announcements")
    public void setAnnouncements(List<Announcement> announcements) {
        this.announcements = announcements;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}
-----------------------------------com.example.SystemLevel.java-----------------------------------

package com.example;

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({
        "Title",
        "Description",
        "AnnouncementType",
        "ActiveF",
        "TileName"
})
public class SystemLevel {

    @JsonProperty("Title")
    private String title;
    @JsonProperty("Description")
    private String description;
    @JsonProperty("AnnouncementType")
    private String announcementType;
    @JsonProperty("ActiveF")
    private Object activeF;
    @JsonProperty("TileName")
    private Object tileName;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("Title")
    public String getTitle() {
        return title;
    }

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

    @JsonProperty("Description")
    public String getDescription() {
        return description;
    }

    @JsonProperty("Description")
    public void setDescription(String description) {
        this.description = description;
    }

    @JsonProperty("AnnouncementType")
    public String getAnnouncementType() {
        return announcementType;
    }

    @JsonProperty("AnnouncementType")
    public void setAnnouncementType(String announcementType) {
        this.announcementType = announcementType;
    }

    @JsonProperty("ActiveF")
    public Object getActiveF() {
        return activeF;
    }

    @JsonProperty("ActiveF")
    public void setActiveF(Object activeF) {
        this.activeF = activeF;
    }

    @JsonProperty("TileName")
    public Object getTileName() {
        return tileName;
    }

    @JsonProperty("TileName")
    public void setTileName(Object tileName) {
        this.tileName = tileName;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

如果您正在创建网络服务,请记住序列化这些;)。

答案 1 :(得分:0)

您可以使用@JsonProperty("FieldName")表示法来映射所有未映射的字段。但是FieldName必须与您的JSON字符串中的确切字段名称匹配。