我有这样的代码:如何解析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
打印地图。它会告诉你什么键和值包含 –