0

我正在开发需要来自 Google 的结果的 java 应用程序。所以我在这个页面搜索 Google Programmatically Java API的第一个答案中使用了帮助

通过成功运行这个程序,我得到了我得到结果的 URL,这没关系。

但我需要解释搜索查询而不是 URL。

那么如何从搜索的查询中获得结果答案,感谢您的帮助。

Answer.java

public static void main(String[] args) throws Exception {
    String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
    String search = "stackoverflow";
    String charset = "UTF-8";

    URL url = new URL(google + URLEncoder.encode(search, charset));
    Reader reader = new InputStreamReader(url.openStream(), charset);
    GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);

    // Show title and URL of 1st result.
    System.out.println(results.getResponseData().getResults().get(0).getTitle());
    System.out.println(results.getResponseData().getResults().get(0).getUrl());
}

GoogleResults.java

public class GoogleResults {

    private ResponseData responseData;
    public ResponseData getResponseData() { return responseData; }
    public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
    public String toString() { return "ResponseData[" + responseData + "]"; }

    static class ResponseData {
        private List<Result> results;
        public List<Result> getResults() { return results; }
        public void setResults(List<Result> results) { this.results = results; }
        public String toString() { return "Results[" + results + "]"; }
    }

    static class Result {
        private String url;
        private String title;
        public String getUrl() { return url; }
        public String getTitle() { return title; }
        public void setUrl(String url) { this.url = url; }
        public void setTitle(String title) { this.title = title; }
        public String toString() { return "Result[url:" + url +",title:" + title + "]"; }
    }

}
4

1 回答 1

1

If you paste the search URL in a browser, you can see how the JSON result is formatted. Your GoogleResult class maps properties from this JSON to properties in this class. To extract more information from the search result, you can just add the appropriate properties to your class, and it should be handled by the JSON parser transforming the JSON result to your GoogleResult class. So if content from the JSON result is the "explanation" you're looking for, your Result class would look like this:

static class Result {
    private String url;
    private String title;
    private String content;
    public String getUrl() { return url; }
    public String getTitle() { return title; }
    public String getContent() {return content; }
    public void setUrl(String url) { this.url = url; }
    public void setTitle(String title) { this.title = title; }
    public void setContent(String content) { this.content = content; }
    public String toString() { return "Result[url:" + url +",title:" + title + ",content:" + content + "]"; }
}    

Then you can use result.getContent() to get the description of the search result. You can use the same process to extract any data from the JSON result that you want.

于 2013-03-30T15:52:20.487 回答