2014-05-19 44 views
4

我想使用Notes代理解析JSON,使用Apache HttpClient獲取JSON。使用JSON.org解析器解析JSON從HttpClient請求

這裏是返回JSON

import lotus.domino.*; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.HttpClientBuilder; 

     Session session = getSession(); 
     AgentContext agentContext = session.getAgentContext(); 

     HttpClient client = HttpClientBuilder.create().build(); 
     HttpGet request = new HttpGet("http://api.acme.com/customer"); 
     request.addHeader("accept", "application/json"); 
     request.addHeader("Host", "api.acme.com"); 
     request.addHeader("X-Api-Version", "1.0"); 
     request.addHeader("Authorization", "Basic ..."); 

     HttpResponse response = client.execute(request);  

的JSON看起來像這樣的代碼。

[ 
    { 
    "id": 123456, 
    "insertDate": "2014-05-12T16:51:38.343", 
    "read": false, 
    "site": "acme.com", 
    "Email": "[email protected]", 
    "location": "/customer/1212?v=1.0" 
    } 
] 

我曾嘗試使用從JSON.org JSONObjectJSONArray,但不能讓它的工作 我需要從json.org包或其他方式來解析JSON一些示例代碼。

+0

當你解析JSON和'toString'的結果時,它應該看起來非常像傳入的JSON。當你解析時你會得到什麼? (你去過json.org並研究過JSON語法嗎?只需要5-10分鐘就可以學習。) –

回答

14

您可以使用HttpResponse#getEntity從HttpResponse中的實體獲取JSON。一旦你的,那麼只需要創建一個新的JSONArray和遍歷數組訪問您的JSON對象中的值:

String json = IOUtils.toString(response.getEntity().getContent()); 
JSONArray array = new JSONArray(json); 
for (int i = 0; i < array.length(); i++) { 
    JSONObject object = array.getJSONObject(i); 
    log.info("the id is {}", object.getInt("id")); 
    log.info("the insertDate is {}", object.getString("insertDate")); 
    log.info("read is {}", object.getBoolean("read")); 
    log.info("the site is {}", object.getString("site")); 
    log.info("the Email is {}", object.getString("Email")); 
    log.info("the location is {}", object.getString("location")); 
} 

我在JSONBlob保存在JSON在http://jsonblob.com/537a43bfe4b047fa2ef5f15d,創造了一個單元測試,請求JSON:

import lombok.extern.slf4j.Slf4j; 
import org.apache.commons.io.IOUtils; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.HttpClientBuilder; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import org.junit.Test; 

@Slf4j 
public class JsonTest { 
    @Test 
    public void test() throws Exception { 
     HttpClient client = HttpClientBuilder.create().build(); 
     HttpGet request = new HttpGet("http://jsonblob.com/api/jsonBlob/537a43bfe4b047fa2ef5f15d"); 
     request.addHeader("accept", "application/json"); 
     HttpResponse response = client.execute(request); 
     String json = IOUtils.toString(response.getEntity().getContent()); 
     JSONArray array = new JSONArray(json); 
     for (int i = 0; i < array.length(); i++) { 
      JSONObject object = array.getJSONObject(i); 
      log.info("the id is {}", object.getInt("id")); 
      log.info("the insertDate is {}", object.getString("insertDate")); 
      log.info("read is {}", object.getBoolean("read")); 
      log.info("the site is {}", object.getString("site")); 
      log.info("the Email is {}", object.getString("Email")); 
      log.info("the location is {}", object.getString("location")); 
     } 
    } 
} 

而且從運行它的輸出是:

11:23:19.508 [main] INFO JsonTest - the id is 123456 
11:23:19.516 [main] INFO JsonTest - the insertDate is 2014-05-12T16:51:38.343 
11:23:19.516 [main] INFO JsonTest - read is false 
11:23:19.516 [main] INFO JsonTest - the site is acme.com 
11:23:19.516 [main] INFO JsonTest - the Email is [email protected] 
11:23:19.516 [main] INFO JsonTest - the location is /customer/1212?v=1.0 

我用IOUtils類InputStream的距離的HttpResponse轉換耳鼻喉科ity,但是無論如何你都可以做到這一點(並且像我這樣轉換它可能不是最好的想法,這取決於JSON的大小)。

+0

謝謝,令人印象深刻的答案。效果很好 –

+0

您應該_never_只將字節解碼爲字符串而不考慮編碼。使用(https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EntityUtils.html#toString(org.apache.http.HttpEntity) –