解决JSONException重复键

我正在使用谷歌自定义搜索引擎并以JSON格式获取结果。对于某些查询,JSON结果具有重复键,因此它产生一个JSONException:重复键“昵称”等。

我正在使用JAVA。

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

可能知道如何解决这个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多次出现

与任何其他对象一样,JSon对象不能具有两个具有相同名称的属性。 这与在地图中两次使用相同密钥的方式相同是非法的。

如果在一个对象中有两个具有相同名称的键,JSONObject将抛出exception。 您可能希望更改对象,以便不在同一对象下重复键。 可能会将nickname视为一个数组。

您需要在问题中粘贴JSON对象。

如果您确实需要此function,请回滚到gson 1.6。 该版本允许重复密钥。

您可以使用Jackson库来解析JSON。 我在使用org.json的软件包时遇到了同样的问题,但我转向jackson,我解决了它: http ://wiki.fasterxml.com/JacksonHome