2013-02-11 184 views
0
ObjectMapper mapper=new ObjectMapper(); 
String response1=client.execute(request1, responseHandler); 
Map jsonObject=mapper.readValue(response1, Map.class); 
jsonObject.get("docs"); 

我从字符串名称response1中的URL获取json字符串并尝试映射它。我的response1字符串如下所示。JSON到Java对象(使用映射)?

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

从这个字符串中,我想制作所有ID的数组。我怎样才能做到这一点?我rying做 jsonObject.get(“文档”),但它没有做任何事情,它说(没有明确的返回值)

回答

1

编辑
使用的数据模型从我GSON例子。现在你可以做,以这种方式:

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例
您应该使用GSON以JSON解析。创建数据模型,帮助您处理“响应”字符串。见我的例子:

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); 
    } 
} 

输出:

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]]] 

和 “文档”,你可以用这种方式获取:

Body body = gson.fromJson(response, Body.class); 
System.out.println(body.getResponse().getDocs()); 
+0

感谢,但我需要它在objectMapping。你有什么想法我可以到达嵌套类。就像在我的例子中,我可以达到响应,但我怎样才能达到文档? – user1964901 2013-02-11 18:20:48