Ich verwende Google API, um Suchergebnisse für bestimmte Abfragen im JSON-Format zu erhalten. Jetzt möchte ich es in JSON-Objekt in Java konvertieren und nur auf bestimmte Werte zugreifen.Google API Suchergebnisse in JSON-Format in JSON-Objekt umgewandelt
Das JSON-Antwort-Format ist:
{
"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,
.
.
Jetzt will ich nur Zugriff "items" Array und mein Code:
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"));
}
Dieser Code NoSuchElementException für "Artikel" gibt. Bitte helfen Sie ...
Danke. Ihre Lösung funktioniert gut. Danke vielmals. – sonam
Gern geschehen. –