2011-02-06 29 views
3

我使用谷歌自定義搜索引擎和獲得的結果JSON format.for某些查詢,JSON結果有重複鍵,因此它產生JSONException:重複鍵「暱稱」等。決心JSONException重複鍵

我正在使用JAVA。

String str=//contains the query result in json format 
JSONObject ob=new JSONObject(str) produces the exception 

可能知道如何解決這個異常?

這裏是JSON回覆:

{ 
    "kind": "customsearch#result", 
    "title": "The World Factbook: India - CIA - The World Factbook", 
    "htmlTitle": "The World Factbook: \u003cb\u003eIndia\u003c/b\u003e -", 
    "link": "https://www.cia.gov/library/publications/the-world-factbook/geos/in.html", 
    "displayLink": "www.cia.gov", 
    "snippet": "Jan 20, 2011 ... Features a map and brief descriptions of geography", 
    "htmlSnippet": "Jan 20, 2011 \u003", 
    "cacheid": "0n2U45w_dvkJ", 
    "pagemap": { 
    "metatags": [ 
    { 
     "il.secur.classif": "UNCLASSIFIED", 
     "il.title": "(U) CIA The World Factbook", 
     "il.summary": "CIA - The World Factbook", 
     "il.cutdate": "20040101", 
     "il.secur.classif": "UNCLASSIFIED", 
     "il.title": "(U) CIA The World Factbook", 
     "il.cutdate": "20040101", 
     "il.secur.classif": "UNCLASSIFIED", 
     "il.pubdate": "20040101", 
     "il.postdate": "20040501", 
     "il.cutdate": "20040101" 
    } 
    ] 
    } 
    } 

這裏il.secur.classif出現多次

+1

`il.secur.classif`,`il.title`,`il.cutdate`重複。 JSON是錯誤的! – Nishant 2011-02-06 10:49:13

回答

7

JSON對象,就像任何其他的對象,不能有兩個屬性具有相同的名稱。這與在地圖上使用相同的密鑰兩次的方式是非法的。

如果在一個對象中有兩個具有相同名稱的鍵,則JSONObject將拋出異常。您可能想要更改您的對象,以便在相同的對象下不會重複鍵。可能考慮將nickname作爲數組。

您需要粘貼問題中的JSON對象。

0

如果您確實需要此功能,請回滾到gson 1.6。該版本允許使用重複密鑰。

0

您可以利用Jackson庫來解析JSON。我會遇到和org.json的軟件包一樣的任務,但是我轉向了Jackson,並且我解決了它:http://wiki.fasterxml.com/JacksonHome

+0

鏈接被破壞? – 2017-04-12 18:57:25