sonam
sonam

Reputation: 905

converting google api search results obtained in json format into json object

I am using google api to get search result for certain queries in JSON format. Now I want to convert it into JSON Object in java and access only particular values.

The JSON response format is :

{
 "kind": "customsearch#search",
 "url": {
  "type": "application/json",
  "template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
 },
 "queries": {
  "nextPage": [
   {
    "title": "Google Custom Search - apple",
    "totalResults": "531000000",
    "searchTerms": "apple",
    "count": 10,
    "startIndex": 11,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "013036536707430787589:_pqjad5hr1a"
   }
  ],
  "request": [
   {
    "title": "Google Custom Search - apple",
    "totalResults": "531000000",
    "searchTerms": "apple",
    "count": 10,
    "startIndex": 1,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "013036536707430787589:_pqjad5hr1a"
   }
  ]
 },
 "context": {
  "title": "Custom Search"
 },
 "searchInformation": {
  "searchTime": 0.206589,
  "formattedSearchTime": "0.21",
  "totalResults": "531000000",
  "formattedTotalResults": "531,000,000"
 },
 "items": [
  {
   "kind": "customsearch#result",
   "title": "Apple",
   "htmlTitle": "\u003cb\u003eApple\u003c/b\u003e",
   "link": "http://www.apple.com/",
   "displayLink": "www.apple.com",
   "snippet": "Apple designs and creates iPod and iTunes, Mac laptop and desktop computers,   the OS X operating system, and the revolutionary iPhone and iPad.",
   "htmlSnippet": "\u003cb\u003eApple\u003c/b\u003e designs and creates iPod and iTunes, Mac laptop and desktop computers, \u003cbr\u003e  the OS X operating system, and the revolutionary iPhone and iPad.",
   "cacheId": "5iRmnZTn43cJ",
   "formattedUrl": "www.apple.com/",
   "htmlFormattedUrl": "www.\u003cb\u003eapple\u003c/b\u003e.com/",
   "pagemap": {
    "cse_image": [
     {
      "src": "http://images.apple.com/home/images/ipad_hero.jpg"
     }
    ],
    "cse_thumbnail": [
     {
      "width": "348",
      "height": "145",
      "src": "https://encrypted-tbn3.google.com/images?q=tbn:ANd9GcQRUCTcMJO12wSHtTA8iXXzdoaHo1ssBW8cyP5ZONgIdpFtr9gNxmRdruk"
     }
    ],
    "metatags": [
     {
      "author": "Apple Inc.",
      "viewport": "width=1024",
      "omni_page": "Apple - Index/Tab"
     }
    ]
   }
  },
  {
   "kind": "customsearch#result",
   "title": "Official Apple Store - Buy the new iPad, Apple TV, 
   .
   .

Now I just want to access "items" array and my code is:

org.json.JSONObject json=null;
json = new JSONObject(jsonResponse);

org.json.JSONObject queryArray=json.getJSONObject("queries");
org.json.JSONArray itemsArray=queryArray.getJSONArray("items");

for(int i=0;i<itemsArray.length();i++)
{
org.json.JSONObject newJSONObj=itemsArray.getJSONObject(i);
System.out.println("Title ::"+newJSONObj.getString("title"));
System.out.println("Link ::"+newJSONObj.getString("link"));
}

This code is giving NoSuchElementException for "items". Please help...

Upvotes: 0

Views: 2184

Answers (1)

Jon O
Jon O

Reputation: 6591

If my brain is parsing this correctly, "queries" does not have "items" within it.

"queries": {
  "nextPage": [
    {
    "title": "Google Custom Search - apple",
    "totalResults": "531000000",
    "searchTerms": "apple",
    "count": 10,
    "startIndex": 11,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "013036536707430787589:_pqjad5hr1a"
    } 
   ],  // end nextpage
"request": [
  {
  "title": "Google Custom Search - apple",
  "totalResults": "531000000",
  "searchTerms": "apple",
  "count": 10,
  "startIndex": 1,
  "inputEncoding": "utf8",
  "outputEncoding": "utf8",
  "safe": "off",
  "cx": "013036536707430787589:_pqjad5hr1a"
  }
 ] // end request
},  // end queries

You want

json.getJSONArray("items"); 

I think.

Upvotes: 2

Related Questions