我有這樣的代碼:如何解析Java中的Google自定義搜索API響應?
public void googleSearch() throws Exception {
URL url = new URL(
"https://www.googleapis.com/customsearch/v1?key=" + key + "&cx=" + cx + "&q=" + qry + "&alt=json&queriefields=queries(request(totalResults))");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
ObjectMapper mapper = new ObjectMapper();
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
Object obj = mapper.readValue(output, Object.class);
System.out.println(obj);
}
conn.disconnect();
}
當我試圖解析來自谷歌使用傑克遜2 JSON響應,來管理數據返回。我只需要圖像鏈接和描述,但是,我根本無法獲取分析的數據。
com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: {; line: 1, column: 1])
at [Source: {; line: 1, column: 3]
如何分開我所需要的圖像鏈接和信息,並使其更易於管理:當我運行這段代碼我得到這個錯誤? (我假設通過解析JSON到一個Java對象,然後能夠調用對象變量,但只是不知道如何)
我該如何檢索JSON中的值?我已閱讀關於使用密鑰的信息,但我不知道Google響應中的密鑰。我試過System.out.println(map.get(「src」)); 但它只是打印null。 – QQuestions
打印地圖。它會告訴你什麼鍵和值包含 –