Java 解析JSONException重复密钥
我正在使用google自定义搜索引擎,并以JSON格式获取结果。对于某些查询,JSON结果具有重复的键,因此它会生成JSONException:重复键“昵称”等 我正在使用JAVAJava 解析JSONException重复密钥,java,json,google-search-api,Java,Json,Google Search Api,我正在使用google自定义搜索引擎,并以JSON格式获取结果。对于某些查询,JSON结果具有重复的键,因此它会生成JSONException:重复键“昵称”等 我正在使用JAVA String str=//contains the query result in json format JSONObject ob=new JSONObject(str) produces the exception 您可能知道如何解决此异常 以下是JSON回复: { "kind": "customsea
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对象与任何其他对象一样,不能有两个同名属性。这是非法的,就像在地图上有两次相同的密钥一样 如果一个对象中有两个同名键,JSONObject将引发异常。您可能需要更改对象,以便在同一对象下不会重复关键点。可能把<代码>昵称< /代码>作为数组。
您需要在问题中粘贴JSON对象。如果您确实需要此功能,请回滚到gson 1.6。该版本中允许使用重复键。您可以使用Jackson库解析JSON。我很想用org.json的包完成与您相同的任务,但我求助于Jackson并解决了它:
il.secur.classif
,il.title
,il.cutdate
重复。JSON是错误的!JSON确实需要唯一的键,但解析器可以选择只返回词法上最后一个重复的成员名来处理此类JSON,如ECMAScript 5.1链接第15.12节(“JSON对象”)中所述。是的,请检查