我是JSON,Google API的新手。所以请指导。
我正在尝试在“ Java”中编写一个应用程序,该应用程序将通过RESTFEL使用Google自定义Serch API。我开始学习JSON,并通过[链接 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的哪个字段将是字符串或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.

我将如何在班上定义它们。 标题字符串或char是什么样的数组,此pagemap是某些惯例或任何站点都可以提供自己的标签。

//类CustomSearch

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;    

}

有帮助吗?

解决方案

如Google的示例中,我使用我的密钥发布了一个真实的搜索:

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

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

items": [
  {
   "kind": "customsearch#result",
   "title": "FTD.COM - Flowers Online | Roses, Fresh Flowers, Plants and Gift ...",
   "htmlTitle": "FTD.COM - \u003cb\u003eFlowers\u003c/b\u003e Online | Roses, Fresh \u003cb\u003eFlowers\u003c/b\u003e, Plants and Gift \u003cb\u003e...\u003c/b\u003e",
   "link": "http://www.ftd.com/",
   "displayLink": "www.ftd.com",
   "snippet": "Aug 2, 2011 ... Order flowers online for same day floral delivery. Shop for flowers, chocolates,   roses, gifts and gift baskets by occasion, season or get beautiful ...",
   "htmlSnippet": "Aug 2, 2011 \u003cb\u003e...\u003c/b\u003e Order \u003cb\u003eflowers\u003c/b\u003e online for same day floral delivery. Shop for \u003cb\u003eflowers\u003c/b\u003e, chocolates, \u003cbr\u003e  roses, gifts and gift baskets by occasion, season or get beautiful \u003cb\u003e...\u003c/b\u003e",
   "cacheId": "D_MQAIEeVpAJ",
   "pagemap": {
    "metatags": [
     {
      "y_key": "e887dc108fef83f6",
      "msvalidate.01": "71957E1C9D33211154243270EB14C63C"
     }
    ]
   }
  ......

看起来像:

items.title     array   The title of the search result, in plain text.

这看起来像是我获得的一堆结果中的字符串数据类型,因此我不确定为什么参考文献将其分类为array

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.

基于 Pagemap描述 这看起来像是本网站可以提供的任意键值对数据。

以下是我从测试中获得的一些pagemaps以供您参考:

 "pagemap": {
    "metatags": [
     {
      "y_key": "e887dc108fef83f6",
      "msvalidate.01": "71957E1C9D33211154243270EB14C63C"
     }
    ]
   }

 "pagemap": {
    "website": [
     {
      "type": "website",
      "title": "ProFlowers",
      "description": "The freshest flowers, guaranteed to last at least 7 days.",
      "image": "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"
     }
    ],
    "metatags": [
     {
      "msnbot": "NOODP",
      "msvalidate.01": "77940E049C181974C3AA656C72688B4C"
     }
    ]
   }

  "pagemap": {
    "metatags": [
     {
      "viewport": "width=device-width; initial-scale=1.0; maximum-scale=1.0;"
     }
    ]

由于pagemap非常非结构化,我会将它们存储为 Map<String, JSONObject> pagemap. 。如您所见,我只是将原始的jsonobject保留在pagemap中,以防万一需要,可以随时提取它。除非有一组定义,否则我们可以在PageMap及其字段中放入哪种类型,而代表PageMap的值可能很困难。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top