1

I am trying to map my json to POJO class with Jackson but i am receiving null values. If i remove @JsonIgnoreProperties annotation then it throws exception of unrecognized property

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
      }
   ]
}

I am getting this json as string and then trying to convert this string to java object.

Here are my POJO classes:

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;
    }

}

My test method to verify if the result has been mapped to json object:

@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);
} 
6
  • 3
    Unrelated: please read about java naming conventions. Field names go camelCase, too. Your habit of going UpperCase confuses everybody else. Commented Aug 15, 2017 at 11:35
  • And then: read about minimal reproducible example. Show a minimal example. Why putting up a 3-layer nested example, when probably something much simpler would do? Commented Aug 15, 2017 at 11:36
  • holy guacomoley. Please edit your question and make all class fields camelCase. It's hard to see now what's a Class and what's a field. Commented Aug 15, 2017 at 11:43
  • 1
    Look at your JSON: it's an object with 3 properties. The value of each property is an array. Each array contains objects, and those objects have 5 properties of type String. So your POJO doesn't have the correct structure at all: it has three properties (OK), which should map toJSON arrays, so should be arrays or Lists (not OK, they are Maps), and the type of the objects inside the list should not be other lists, but SPOAnnouncements_Properties. Commented Aug 15, 2017 at 11:44
  • @JB Nizet: Thank you so much for prompt guidance. Can you please propose the code as I tried it but was not successful. Here is what I tried: private Map<String, List<SPOAnnouncements_Properties>> TileLevel; Commented Aug 15, 2017 at 12:04

2 Answers 2

1

Your POJO structures are invalid. Check out http://www.jsonschema2pojo.org - an online tool for generating Java POJOs from JSON.

Your classes should look more or less like below, but you will need to replace ActiveF and TileName fields with preferred types.

-----------------------------------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);
    }

}

Remember to Serialize those if you're creating a webservice ;).

Sign up to request clarification or add additional context in comments.

Comments

0

You can use @JsonProperty("FieldName") notation to map all the fields that are not getting mapped. But FieldName must match with exact field name in your JSON string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.