2015-12-07 41 views
0

我想分析Json響應:如何從HttpResponse對象解析JSON數據

client = new DefaultHttpClient(); 
HttpGet request = new HttpGet(url); 
HttpResponse response = client.execute(request); 

任何建議如何做到這一點?

+6

可能的重複[如何從Java HTTPResponse解析JSON?](http://stackoverflow.com/questions/2845599/how-do-i-parse-json-from-a-java-httpresponse ) – Raf

回答

2

您可以使用JSON-簡單

https://code.google.com/p/json-simple/

如果你用maven

<dependency> 
    <groupId>com.googlecode.json-simple</groupId> 
    <artifactId>json-simple</artifactId> 
    <version>1.1</version> 
</dependency> 

然後在你的代碼

JSONParser jsonParser = new JSONParser(); 
    JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); 
    // get a String from the JSON object 
    String firstName = (String) jsonObject.get("firstname"); 
    System.out.println("The first name is: " + firstName); 

在這裏有一個例子

http://examples.javacodegeeks.com/core-java/json/java-json-parser-example/