2011-09-04 102 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,以防萬一你需要它,你總是可以提取它。除非有一組定義,我們可以將頁面地圖與其字段一起放入什麼類型,將頁面地圖的值表示爲類可能很困難。