2012-04-02 78 views
0

我正在使用google api獲取JSON格式的某些查詢的搜索結果。現在我想將它轉換成java中的JSON對象並只訪問特定的值。將json格式的google api搜索結果轉換爲json對象

JSON響應格式爲:

{ 
"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, 
    . 
    . 

現在我只想訪問 「項目」 陣列和我的代碼是:

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

這段代碼是給NoSuchElementException異常的 「項目」。 請幫忙...

回答

2

如果我的大腦正確地解析這個,「查詢」裏面沒有「項目」。

"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 

你想

json.getJSONArray("items"); 

我想。

+1

謝謝。你的解決方案工作正常。非常感謝。 – sonam 2012-04-02 16:49:07

+0

不客氣。 – 2012-04-02 17:01:11