1

我对 json、google apis 很陌生。所以请指导。
我正在尝试在“JAVA”中编写一个应用程序,该应用程序将通过 Restful 使用谷歌自定义 serch api。我开始学习 json 并通过 [link] http://code.google.com/apis/customsearch/v1/overview.html我想写一些代码。

这显示了搜索 google 的 json:
http ://code.google.com/apis/customsearch/v1/reference.html#method_search_cse_list

参考是http://code.google.com/apis/customsearch/v1/reference.html

从参考资料中,我发现此 CustomSearch 的哪些字段将是 String 或 int 或任何其他数据类型。他们还定义了每个对象的结构。

但我面临一些数据类型的问题:

items.title     array   The title of the search result, in plain text.
items.snippet   array   The snippet of the search result, in plain text.
items.pagemap   object  Contains pagemap information for this search result.    
items.pagemap.value     array   Pagemap information, keyed by the name of this pagemap.     
items.pagemap.value.value   object  The actual pagemap information.

我将如何在我的课堂上定义它们。什么样的数组是标题字符串或字符,这个页面地图是一些约定,或者任何网站都可以给出自己的标签。

// 自定义搜索类

public class CustomSearch {
public URL getURL() throws MalformedURLException{
    return url.getURL();
}

@Key ("items") ArrayList<SearchResult> results;
private @Key SearchURL url;
private @Key Query queries; 

}

// 班级

class SearchResult {
public SearchResult(){        
}

public String getTitle(){
    return title;
}
public String getLink(){
    return link;
}
public String getSnippet(){
    return snippet;
}

private @Key String title;   // is this right ?
private @Key String htmlTitle;
private @Key String link;
private @Key String snippet;   // is this right ?
private @Key String htmlSnippet;    

}

4

1 回答 1

1

按照 Google 示例中的建议,我使用我的密钥进行了真正的搜索:

获取 https://www.googleapis.com/customsearch/v1?key=INSERT-YOUR-KEY&cx=013036536707430787589:_pqjad5hr1a&q=flowers&alt=json

这就是我得到的(仅显示一些数据)

项目”: [
  {
   "kind": "customsearch#result",
   "title": "FTD.COM - 鲜花在线 | 玫瑰、鲜花、植物和礼物......",
   "htmlTitle": "FTD.COM - \u003cb\u003eFlowers\u003c/b\u003e 在线 | 玫瑰,新鲜 \u003cb\u003eFlowers\u003c/b\u003e,植物和礼物 \u003cb\u003e...\u003c/b\ u003e",
   "链接": "http://www.ftd.com/",
   "displayLink": "www.ftd.com",
   "snippet": "2011 年 8 月 2 日...在线订购鲜花,当天送花。按场合、季节或变美购买鲜花、巧克力、玫瑰、礼品和礼品篮...",
   "htmlSnippet": "2011 年 8 月 2 日 \u003cb\u003e...\u003c/b\u003e 在线订购 \u003cb\u003eflowers\u003c/b\u003e 以实现当日鲜花配送。购买 \u003cb\u003eflowers\u003c/b \u003e、巧克力、\u003cbr\u003e 玫瑰、礼物和礼品篮,按场合、季节或变美\u003cb\u003e...\u003c/b\u003e",
   "cacheId": "D_MQAIEeVpAJ",
   “页面地图”:{
    “元标签”:[
     {
      "y_key": "e887dc108fef83f6",
      “msvalidate.01”:“71957E1C9D33211154243270EB14C63C”
     }
    ]
   }
  ……

看起来像:

items.title 数组 搜索结果的标题,纯文本。

这看起来像是我得到的一堆结果中的字符串数据类型,所以我不确定为什么引用将它分类为数组

items.snippet array 搜索结果的片段,纯文本。

从我得到的结果来看,这看起来也像 String 数据类型

items.pagemap 对象 包含此搜索结果的页面地图信息。    
items.pagemap.value 数组 Pagemap 信息,以该 pagemap 的名称为键。     
items.pagemap.value.value 对象 实际的页面地图信息。

根据PageMap 描述,这看起来像是网站可以提供的任意键值对数据。

以下是我从测试中获得的一些页面地图供您参考:

“页面地图”:{
    “元标签”:[
     {
      "y_key": "e887dc108fef83f6",
      “msvalidate.01”:“71957E1C9D33211154243270EB14C63C”
     }
    ]
   }

 “页面地图”:{
    “网站”: [
     {
      “类型”:“网站”,
      "title": "ProFlowers",
      "description": "最新鲜的花朵,保证至少持续7天。",
      "图片": "http://a1128.g.akamai.net/7/1128/497/0001/images.proflowers.com/pcsite/ProflowersLogo_nb.gif",
      "url": "http://www.proflowers.com/",
      "site_name": "ProFlowers",
      “app_id”:“180475245301608”
     }
    ],
    “元标签”:[
     {
      "msnbot": "NOODP",
      “msvalidate.01”:“77940E049C181974C3AA656C72688B4C”
     }
    ]
   }

  “页面地图”:{
    “元标签”:[
     {
      “视口”:“宽度=设备宽度;初始比例=1.0;最大比例=1.0;”
     }
    ]

由于页面地图非常非结构化,我会将它们存储为Map<String, JSONObject> pagemap. 如您所见,我只是将原始 JSONObject 保留在 pagemap 中,因此如果您需要它,您可以随时提取它。除非有一组定义,我们可以在页面映射及其字段中放入什么类型,否则将页面映射的值表示为一个类可能很困难。

于 2011-09-07T12:15:56.863 回答