1

I am using Jackson library to try and parse my JSON file. My JSON is actually an ARRAY of JSON Objects:

JSON ARRAY:

[
 {
   "Id" : "0",
  "name" : "John"
},
{
   "Id" : "1",
   "name" : "Doe"
}
]

POJO CLASS:

@JsonIgnoreProperties(ignoreUnknown = true)
public class QuestData {
    private String Id;

    private String name;

    public String getId() {
        return Id;
    }

   public String getName() {
       return name;
   }
}

PARSING JSON:

  private void parseJSON(File jsonFile) {
        try {
            byte[] jsonData = Files.readAllBytes(jsonFile.toPath());
            System.out.println(new String(jsonData));

            ObjectMapper mapper = new ObjectMapper();

            List<QuestData> questDataList = mapper.readValue(jsonData, mapper.getTypeFactory().constructCollectionType(List.class, QuestData.class));
            System.out.println("Read values: " + questDataList.get(0).getId());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

My first print statement prints correct Json data back (as String).

But next print statement says NULL. I even tried to itreate over entire list ot see if there is something that is not null, but with no luck.

I do not know what I am doing wrong here.

AndroidDev101
  • 122
  • 1
  • 13
  • 1
    can you provide the code for Class QuestData – Joram Jun 14 '17 at 12:00
  • I think you're looking for that: https://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects, if this does not answer your problem, it's likely to be in your POJO QuestData, as @Joram mentionned it. – sjahan Jun 14 '17 at 12:03
  • @Joram Added POJO class – AndroidDev101 Jun 14 '17 at 12:07
  • @sjahan that link I used to create the code I posted in question. – AndroidDev101 Jun 14 '17 at 12:09
  • The `Id` must be capitalized? If you lowercase the `id` in the `JSON` and the `QuestData` class your code works. – jeojavi Jun 14 '17 at 12:10
  • Shoot.. I get the data with capital. What I do before is to convert that data from CSV file to JSON. Maybe in that conversion I can affect capitalization. – AndroidDev101 Jun 14 '17 at 12:12

2 Answers2

2

Jackson will by default use setter methods to set fields. So add setters like:

@JsonProperty("Id")  // otherwise Jackson expects id for setId
public void setId(String id) {
    Id = id;
}

Alternatively, tell Jackson to look for fields with this config:

mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

In this case Jackson will match the name of the field in the class Id with the one in JSON Id

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
0

Just add the @JsonProperty annotation to the Id property in your QuestData class:

@JsonIgnoreProperties(ignoreUnknown = true)
public class QuestData {

    @JsonProperty("Id")
    private String Id;

    private String name;

    public String getId() {
        return Id;
    }

   public String getName() {
       return name;
   }
}
jeojavi
  • 876
  • 1
  • 6
  • 15