如何从Google自定义Api搜索返回的Json中获取Java对象

如何从Google自定义Api搜索返回的Json中获取Java对象,java,json,google-custom-search,Java,Json,Google Custom Search,我有以下代码用于通过google自定义api检索web搜索结果 package google.custom.api.results.google.custom.api; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLCon

我有以下代码用于通过google自定义api检索web搜索结果

package google.custom.api.results.google.custom.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import com.google.gson.Gson;

public class handler {


    public static boolean searchAndSaveGoogleCustomSearch() throws  UnsupportedEncodingException 
    {
        String apiKey="AIzaSyB21aUCd8HYMsHgo7APH-98ah-8tLgkPFM";
        String cxId="005621018181405156379:yvdukowvdte";

        String keyToSearch="News";


        String urlToSearch="https://www.googleapis.com/customsearch/v1?key=" +apiKey+ "&cx="+cxId
                +"&alt=json"+"&q="+keyToSearch;


        try {
            URL url=new URL(urlToSearch);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
              conn.setRequestMethod("GET");
              conn.setRequestProperty("Accept", "application/json");
              BufferedReader br = new BufferedReader(new InputStreamReader ( ( conn.getInputStream() ) ) );
              GoogleCustomApiResult result = new Gson().fromJson(br, GoogleCustomApiResult.class);
              System.out.println(result);
              conn.disconnect();


        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
    }


    public static void main(String[] args) throws UnsupportedEncodingException {

            searchAndSaveGoogleCustomSearch();
            System.out.println("Google Crawl done.");
    }
}
下面是我如何尝试从json结果中检索java对象

package google.custom.api.results.google.custom.api;

import java.util.List;

public class GoogleCustomApiResult 
{

      private String link;
      private String htmlFormattedUrl;

      private List<GoogleCustomApiResult> items;

      public String getLink() {
        return link;
      }

      public String getUrl() {
        return htmlFormattedUrl;
      }

      public void setUrl(String htmlFormattedUrl) {
        this.htmlFormattedUrl = htmlFormattedUrl;
      }

      public List<GoogleCustomApiResult> getItems() {
        return items;
      }

      public void setLink(String link) {
        this.link = link;
      }

      public void setGroups(List<GoogleCustomApiResult> items) {
        this.items = items;
      }

      public void getThing (int i) {
        System.out.println(items.get(i));
      }

      public String getLink(int i) {
        return items.get(i).toString();
      }

      public String toString() {
        return String.format("%s", link);
      }  
}
但是每次返回的java对象都是null。我是Json新手,不知道如何解析结果以获得填充值的java对象。
Url正在返回结果,但值不会进入java对象。请帮帮我。

查看您的回复,您没有映射与您的模型完全相同的json结构
GoogleCustomApireult
,因为缺少很多字段

您有两个选择:

  • 精确映射json结构(不要尝试这样做,因为你有一个巨大的json!)

  • 解析响应以获取所需对象的列表(推荐)

  • 看看Gson API中的
    JsonParser


    还要更正@Maraboc指出的模型类中的错误。

    查看您的响应,您没有映射与您的模型完全相同的json结构
    GoogleCustomApireult
    ,因为缺少很多字段

    您有两个选择:

  • 精确映射json结构(不要尝试这样做,因为你有一个巨大的json!)

  • 解析响应以获取所需对象的列表(推荐)

  • 看看Gson API中的
    JsonParser


    同时纠正@Maraboc指出的模型类中的错误。

    我尝试了完整的URL,得到了一个错误代码json:line System.out.println(result)是什么;在“searchAndSaveGoogleCustomSearch()”方法中,“打印输出”是
    GoogleCustomApirect
    类中
    项的类型,等于
    List
    而不是
    List
    ?.out.println(结果)打印以下内容:GoogleCustomApirect[title=null,link=null,snippet=null,cacheId=null,formattedUrl=null,htmlFormattedUrl=null]现在试试这个url,它会很好地工作。我尝试了完整的url,得到了一个错误代码json:System.out.println(result)行是什么;在方法“searchAndSaveGoogleCustomSearch()中“print out”(打印输出)是
    GoogleCustomApirect
    类中
    项的类型,等于
    List
    而不是
    List
    ?System.out.println(结果)打印以下内容:GoogleCustomApirect[title=null,link=null,snippet=null,cacheId=null,formattedUrl=null,htmlFormattedUrl=null]现在试试url,它可以正常工作
    package com.til.et.mynewsletter.core.parser.json.google;
    
    import java.util.List;
    
    public class CustomApiResult 
    {
        private String kind;
        private String title;
        private String htmlTitle;
        private String link;
        private String displayLink;
        private String snippet;
        private String htmlSnippet;
        private String cacheId;
        private String formattedUrl;
        private String htmlFormattedUrl;
        //private String htmlSnippet;
    
    
        public String getKind() {
            return kind;
        }
        public void setKind(String kind) {
            this.kind = kind;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getHtmlTitle() {
            return htmlTitle;
        }
        public void setHtmlTitle(String htmlTitle) {
            this.htmlTitle = htmlTitle;
        }
        public String getLink() {
            return link;
        }
        public void setLink(String link) {
            this.link = link;
        }
        public String getDisplayLink() {
            return displayLink;
        }
        public void setDisplayLink(String displayLink) {
            this.displayLink = displayLink;
        }
        public String getSnippet() {
            return snippet;
        }
        public void setSnippet(String snippet) {
            this.snippet = snippet;
        }
        public String getHtmlSnippet() {
            return htmlSnippet;
        }
        public void setHtmlSnippet(String htmlSnippet) {
            this.htmlSnippet = htmlSnippet;
        }
        public String getCacheId() {
            return cacheId;
        }
        public void setCacheId(String cacheId) {
            this.cacheId = cacheId;
        }
        public String getFormattedUrl() {
            return formattedUrl;
        }
        public void setFormattedUrl(String formattedUrl) {
            this.formattedUrl = formattedUrl;
        }
        public String getHtmlFormattedUrl() {
            return htmlFormattedUrl;
        }
        public void setHtmlFormattedUrl(String htmlFormattedUrl) {
            this.htmlFormattedUrl = htmlFormattedUrl;
        }
        @Override
        public String toString() {
            return "GoogleCustomApiResult [title=" + title + ", link=" + link + ", snippet=" + snippet + ", cacheId="
                    + cacheId + ", formattedUrl=" + formattedUrl + ", htmlFormattedUrl=" + htmlFormattedUrl + "]";
        }