JSONをJavaのクラスに変換する際の問題(Google Custom Search)
-
22-10-2019 - |
質問
私はJSON、Google APIに非常に新しいです。ガイドしてください。
Restfulを介してGoogle Custom Serch APIを使用する「Java」でアプリケーションを作成しようとしています。私は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
参照から、この慣習のどのフィールドが文字列または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はコンベンションまたはどのサイトでも独自のタグを提供できます。
// class 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.
これは、私が得た結果の束の文字列データ型のように見えますので、参照がそれを配列として分類した理由がわかりません
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説明 これは、Webサイトが提供できる任意のキー価値ペアデータのように見えます。
以下は、あなたの参照のために私のテストから得られるページマップのいくつかです。
"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に保持しているだけなので、必要に応じていつでも抽出できます。一連の定義がない限り、そのフィールドと一緒にページマップに入れることができるタイプは、クラスとしてのページマップの価値を表すのは難しいかもしれません。