我使用谷歌自定义搜索引擎,并获得在JSON format.for某些查询的结果,JSON结果有重复的关键字,因此它产生了一个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会多次出现
发布于 2011-02-06 16:59:19
与任何其他对象一样,JSon对象不能有两个同名属性。这是非法的,就像在映射中有两次相同的键一样。
如果在一个对象中有两个同名的键,JSONObject会抛出异常。您可能希望更改对象,以便在同一对象下不会重复关键点。也许可以将nickname看作一个数组。
您需要在问题中粘贴JSON对象。
发布于 2012-10-12 01:59:04
如果您确实需要此功能,请回滚到gson 1.6。该版本中允许使用重复的键。
发布于 2014-12-04 20:42:39
您可以使用Jackson库来解析JSON。我在使用org.json的包做同样的任务时遇到了问题,但是我求助于Jackson并解决了这个问题:http://wiki.fasterxml.com/JacksonHome
https://stackoverflow.com/questions/4912386
复制相似问题