我正在使用谷歌自定义搜索引擎并以 JSON 格式获取结果。对于某些查询,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 出现了多次
最佳答案
与其他任何对象一样,JSon 对象不能有两个同名的属性。这与在 map 中两次使用相同的键一样是非法的。
如果您在一个对象中有两个同名的键,JSONObject 将抛出异常。您可能想要更改您的对象,以便键不会在同一对象下重复。可能将 nickname
视为一个数组。
您需要将 JSON 对象粘贴到问题中。
关于java - 解决 JSONException 重复键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4912386/