1

The structure of JSON looks like:

{
 "id": "0001",
 "type": "portable",
 "name": "mobile",
 "results":[
   { 
    "company": "Apple",
    "country": "US", 
    "city": "Cupertino"
   },
   { 
    "company": "Google",
    "country": "Japan", 
    "city": "Tokyo"
   }
  ]
}

I tried to map the above json to my "Response" class but I could only retrieve id, type and name. For json object "results" is shows results:[]. Here is the code I have implemented;

CompanyDetail class:

@JsonIgnoreProperties(ignoreUnknown = true)
public class CompanyDetail {
    public String company;
    public String country;
    public String city;

   //getters and setters
}

Company class:

    @JsonIgnoreProperties(ignoreUnknown = true)
public class CompanyD {
    public String name;
    public List<Detail> result;

   //getters and setters
}

Implementation class:

  @Override
  public ResponseEntity<String> getResponseEntity() {       
      RestTemplate restTemplate = new RestTemplate();
      ResponseEntity<String> response = restTemplate .getForEntity(
      "url", String.class);       
      logger.info("Response:"+response);
      return response;
}

    @Override
    public Company getResponseObject() {
        RestTemplate restTemplate=new RestTemplate();
        Company comp=(Company) restTemplate.getForObject("url",
                     Company.class,200);
        List<CompanyDetail> companyInfo = comp.getCompanyDetail();
        for (CompanyDetail companyDetail : companyInfo) {
            logger.info("Country:"+companyDetail.getCountry());
            logger.info("City:"+companyDetail.getCity());
            logger.info("Company:"+companyDetail.getCompany());
        }
    return comp;

From both the implementation method I am getting null values for the "results" json object. I could not figure out what is wrong with my code.

Thanks in advance.

  • Possible Duplicate of [this](http://stackoverflow.com/questions/28893713/map-nested-json-objects-to-java-classes-with-spring-resttemplate?rq=1) – Abhijeet Feb 14 '17 at 08:16

1 Answers1

0

I'm having the same issue however in your case it looks like you misspelled "resluts".

Jason
  • 1