2011-09-04 74 views
1

我对json,google apis很新。所以请指导。
我想在'JAVA'中编写一个应用程序,通过Restful使用谷歌自定义serch api。我开始学习JSON并通过[链接] http://code.google.com/apis/customsearch/v1/overview.html我想写一些代码。json转换为java类的问题(谷歌自定义搜索)

这说明搜索的谷歌的JSON:
http://code.google.com/apis/customsearch/v1/reference.html#method_search_cse_list

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

从i找到的参考此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;  

}

+0

你用什么来反序列化JSON响应? – momo

+0

我thik我会使用Gson。 –

+0

@momo使用任何反序列化器都会对此问题产生任何影响。 –

回答

1

我发出用我的钥匙一个真正的搜索如谷歌的例子表明:

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.

这看起来像一堆结果的字符串数据类型,我得到的,所以我不知道为什么参考将它归类为数组

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

这看起来像一个字符串数据类型以及从结果我基础上PageMap description这看起来像一个任意键值对的数据,该网站可以提供了

 
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. 

下面是一些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;" 
    } 
    ] 

由于页映射非常非结构化我想将它们存储为Map<String, JSONObject> pagemap。正如你所看到的,我只保留pagemap中的原始JSONObject,以防万一你需要它,你总是可以提取它。除非有一组定义,我们可以将页面地图与其字段一起放入什么类型,将页面地图的值表示为类可能很困难。