1

I am unable to convert the given Json String to java Object

Validated the Json format, it is correct.

@JsonIgnoreProperties(ignoreUnknown = true)
public class DevPol {

    private String id;
    private Header header;

    //Setters and Getters
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Header {

    private String name;
    private String lastUpdate;
    private int priority;
    private boolean active;

    //Setters and Getters
}

import com.fasterxml.jackson.databind.ObjectMapper;
public class ConvertJsonToJava {

    static String apiResult = "[ {\"Id\":\"5899503ad06f7f0008817430\",  \"Header\":{  \"name\":\"ClCol\"," + 
            "         \"lastupdate\":\"2017-02-07T04:42:34.654Z\", \"priority\":1,  \"active\":true } }," + 
            "   { \"Id\":\"5899503ad06f7f0008817431\",\"Header\":{  \"name\":\"SysPol\"," +
            " \"lastupdate\":\"2017-02-07T04:42:34.659Z\", \"priority\":2, \"active\":true } }]"; 

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        DevPol[] devPOlArr = mapper.readValue(apiResult, DevPol[].class);
        for(DevPol devPol: devPOlArr) {

            System.out.println(devPol.getId());
        }
    }

}

I expected the output to be Id values but,the result is null null

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Vamsi
  • 13
  • 4
  • Possible duplicate of [How to convert the following json string to java object?](https://stackoverflow.com/questions/10308452/how-to-convert-the-following-json-string-to-java-object) – Ramesh Oct 06 '19 at 05:46
  • You should mention JSON properties at your bean properties... – Rookie007 Oct 06 '19 at 06:01

1 Answers1

3

The issue is upper-case letters in json field names and java class fields.

If it is possible rename 'Id' -> 'id' in json and java. Otherwise you should add json property names to java fields:

public class DevPol {

    @JsonProperty("Id")
    private String Id;
    @JsonProperty("Header")
    private Header Header;

//Setters and Getters

}
i.bondarenko
  • 3,442
  • 3
  • 11
  • 21