Question

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;    

}

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top