1

JSON 的结构如下所示:

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

我试图将上面的 json 映射到我的“响应”类,但我只能检索 id、类型和名称。对于 json 对象“结果”显示结果:[]。这是我实现的代码;

公司详情类:

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

   //getters and setters
}

公司等级:

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

   //getters and setters
}

实现类:

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

从这两种实现方法中,我都得到了“结果”json对象的空值。我无法弄清楚我的代码有什么问题。

提前致谢。

4

1 回答 1

0

我遇到了同样的问题,但是在您的情况下,您似乎拼错了“resluts”。

于 2017-02-13T21:38:24.510 回答