0

I would like to ask how to properly convert the next json string. 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"},...

and I would like only the "docs" array. I am using Jackson Converter in this way:

    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:

@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

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?

4
  • 1
    You need to parse from the top of the JSON, you cant just extract docs out of the middle. Where is the Response class, for example, that would contain a Docs class and a Meta class? Commented Apr 5, 2016 at 15:57
  • I don't have much experience with JSON, so I didn't know I should do that. Commented Apr 5, 2016 at 16:01
  • How did you know to create the Docs and WebUrl class, then? Just copy your JSON into this site and it can generate Jackson classes for you. jsonschema2pojo.org Commented Apr 5, 2016 at 16:03
  • Thank you, this clarified a lot of things for me. Commented Apr 5, 2016 at 16:27

1 Answer 1

2

You need to have classes Like below. as @cricket_007 mentioned you can not extract something out of middle.

Result.Java

import com.fasterxml.jackson.annotation.JsonProperty;

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

Response.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

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I worked perfectly like this. And this also clarified a lot for me. Thanks.
List<Object> YES! Object[] NO;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.