在java中将json转换为类时出现问题(谷歌自定义搜索)

发布于 2024-12-02 22:13:55 字数 1975 浏览 1 评论 0 原文

我对 json、google apis 很陌生。所以请指导。
我正在尝试用“JAVA”编写一个应用程序,该应用程序将通过 Restful 使用 google 自定义 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;    

}

I am very new to json, google apis. So please guide.
I am trying to write an application in 'JAVA' that would use google custom serch api through Restful. I started learning json and going through [link] http://code.google.com/apis/customsearch/v1/overview.html i wanted to write some code.

This shows the json of search for google:
http://code.google.com/apis/customsearch/v1/reference.html#method_search_cse_list

reference is http://code.google.com/apis/customsearch/v1/reference.html

From the reference i found which fields of this CustomSearch would be String or int or any other data type. They have also defined structure of every object.

But i am facing problems with some data types:

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.

How would i define them in my class. what kind of array is title string or char and this pagemap is some convention or any site can give its own tags .

// 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

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;    

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友欢 2024-12-09 22:13:56

我按照谷歌示例中的建议使用我的密钥发出了真正的搜索:

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 描述 这看起来像是网站可以提供的任意键值对数据。

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

 "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页面地图。正如您所看到的,我只是将原始 JSONObject 保留在页面映射中,因此如果您需要它,您可以随时提取它。除非有一组定义,我们可以将什么类型及其字段放入页面映射中,否则将页面映射的值表示为类可能很困难。

I issued a real search using my key as suggested in Google's example:

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

And here is what I get (showing just bit of data)

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"
     }
    ]
   }
  ......

It looks like:

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

This looks like a String datatype from bunch of results that I get, so I am not sure why the reference classified it as array

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

This looks like a String datatype as well from the results that I got

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.

Based on the PageMap description this looks like an arbitrary key-value pair data that the website could provide.

Below are some of pagemaps that I get from my test for your reference:

 "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;"
     }
    ]

As pagemap is very unstructured I would store them as Map<String, JSONObject> pagemap. As you can see that I just keep the original JSONObject in pagemap so in case if you need it, you could always extract it. Unless there is a set of definitions what type we could put in pagemap along with its fields, representing the value of the pagemap as a Class could be difficult.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文