简体   繁体   English

如何正确解析json对象以用Jackson数组?

[英]How to properly parse json objects to array with Jackson?

I would like to ask how to properly convert the next json string. 我想问一下如何正确转换下一个json字符串。 I have this string: 我有这个字符串:

{"response":{"meta":{"hits":1326,"time":55,"offset":0},"docs":[{"web_url":"http://www.nytimes.com/2016/04/05/fashion/china-luxury-goods-retail.html"},{"web_url":"http://www.nytimes.com/2016/04/05/fashion/luxury-goods-retail.html"},... {“ response”:{“ meta”:{“ hits”:1326,“ time”:55,“ offset”:0},“ docs”:[{“ web_url”:“ http://www.nytimes.com /2016/04/05/fashion/china-luxury-goods-retail.html"},{"web_url":"http://www.nytimes.com/2016/04/05/fashion/luxury-goods-retail .html“},...

and I would like only the "docs" array. 我只想要“ docs”数组。 I am using Jackson Converter in this way: 我以这种方式使用Jackson Converter:

    RestTemplate restTemplate=new RestTemplate();
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    jsonConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "json", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET)));
    restTemplate.getMessageConverters().add(jsonConverter);
    ArrayList<WebUrl> topStories=restTemplate.getForObject(URL,Docs.class).getUrls();

and here is my Docs class: 这是我的Docs类:

@JsonIgnoreProperties(ignoreUnknown = true)

public class Docs {
  @JsonProperty("docs")
  private ArrayList<WebUrl> urls;

  public Docs(ArrayList<WebUrl> urls) {
     this.urls= urls;
  }

  public Docs(){

  }

  public ArrayList<WebUrl> getUrls() {
    return urls;
  }

  public void setUrls(ArrayList<WebUrl> urls) {
    this.urls=urls;
  }

 }

And here is my WebUrl.class 这是我的WebUrl.class

public class WebUrl {

@JsonProperty("web_url")
private String webUrl;

public WebUrl(){

}

public String getWebUrl() {
    return webUrl;
}

public void setWebUrl(String webUrl) {
    this.webUrl = webUrl;
}

public WebUrl(String webUrl) {

    this.webUrl = webUrl;
}
}

But it gives me null pointer exception on the array. 但这给了我数组上的空指针异常。 How can I fix this? 我怎样才能解决这个问题?

You need to have classes Like below. 您需要具有如下类。 as @cricket_007 mentioned you can not extract something out of middle. 正如@ cricket_007提到的,您不能从中间提取内容。

Result.Java 结果

import com.fasterxml.jackson.annotation.JsonProperty;

 public class Result {
    @JsonProperty("response")
    public Response response;
}

Response.Java 响应Java

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.List;

public class Response {
    @JsonProperty("docs")
    public List<Doc> docs = new ArrayList<Doc>();
}

Doc.java Doc.java

import com.fasterxml.jackson.annotation.JsonProperty;

public class Doc {
    @JsonProperty("web_url")
    public String webUrl;
}

Once you have these classes, you can call the service like below. 一旦有了这些类,就可以像下面这样调用该服务。

Result forObject = restTemplate.getForObject("yourURL", Result.class);

And then, from result you can extract the list of URLs. 然后,从结果中可以提取URL列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM