0
 ObjectMapper mapper=new ObjectMapper();
 String response1=client.execute(request1, responseHandler);
 Map jsonObject=mapper.readValue(response1, Map.class);
 jsonObject.get("docs");

I am getting json string from a url in a string name response1 and trying to map it. My response1 string looks like following.

{"response":
 {"numFound":5,
  "start":0,
  "docs":[
   {"id":1},
   {"id":2},
   {"id":3},
   {"id":4},
   {"id":5}
   ]
  }
 }

From this string i want to make array of all the ids. How can i do that? I am rying to do jsonObject.get("docs") but it does not do anything and it says (no explicit return value)

1 Answer 1

1

EDIT
Use data model from my GSON example. Now you can do that in this way:

String response = "{\"response\":{\"numFound\":5,\"start\":0,\"docs\":[{\"id\":1},{\"id\":2},{\"id\":3},{\"id\":4},{\"id\":5}]}}";
ObjectMapper mapper = new ObjectMapper();
ObjectReader objectReader = mapper.reader(Body.class);
Body body = (Body) objectReader.readValue(response);
System.out.println(body.getResponse().getDocs());

GSON EXAMPLE
You should use GSON to JSON parsing. Create data model which help you working with "response" string. See my example:

import java.util.Arrays;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class SourceCodeProgram {

    public static void main(String[] args) throws Exception {
        String response = "{\"response\":{\"numFound\":5,\"start\":0,\"docs\":[{\"id\":1},{\"id\":2},{\"id\":3},{\"id\":4},{\"id\":5}]}}";
        Gson gson = new GsonBuilder().serializeNulls().create();

        System.out.println("To JSON");
        System.out.println(gson.toJson(createTestBody()));
        System.out.println("From JSON");
        System.out.println(gson.fromJson(response, Body.class));
    }

    private static Body createTestBody() {
        Response response = new Response();
        response.setNumFound(5);
        response.setStart(0);
        response.setDocs(Arrays.asList(new Doc(1), new Doc(2), new Doc(3),
                new Doc(4), new Doc(5)));

        Body body = new Body();
        body.setResponse(response);
        return body;
    }
}

class Body {

    private Response response;

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response) {
        this.response = response;
    }

    @Override
    public String toString() {
        return "Body [response=" + response + "]";
    }
}

class Response {

    private int numFound;
    private int start;
    private List<Doc> docs;

    public int getNumFound() {
        return numFound;
    }

    public void setNumFound(int numFound) {
        this.numFound = numFound;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public List<Doc> getDocs() {
        return docs;
    }

    public void setDocs(List<Doc> docs) {
        this.docs = docs;
    }

    @Override
    public String toString() {
        return "Response [numFound=" + numFound + ", start=" + start
                + ", docs=" + docs + "]";
    }
}

class Doc {

    private int id;

    public Doc() {
    }

    public Doc(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return Integer.toString(id);
    }
}

Output:

To JSON
{"response":{"numFound":5,"start":0,"docs":[{"id":1},{"id":2},{"id":3},{"id":4},{"id":5}]}}
From JSON
Body [response=Response [numFound=5, start=0, docs=[1, 2, 3, 4, 5]]]

And "docs" you can retrieve in this way:

Body body = gson.fromJson(response, Body.class);
System.out.println(body.getResponse().getDocs());
Sign up to request clarification or add additional context in comments.

1 Comment

thanks but i need it in objectMapping. Do you have any idea how i can get to the nested class. like in my example i can reach to response but how can i reach to docs?

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.