【发布时间】:2011-06-22 04:39:36
【问题描述】:
我正在使用谷歌自定义搜索引擎并以 JSON 格式获取结果。对于某些查询,JSON 结果具有重复键,因此会产生 JSONException: Duplicate key "nickname" 等。
我正在使用 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 出现多次
【问题讨论】:
-
il.secur.classif,il.title,il.cutdate重复。 JSON 错误! -
JSON 确实需要唯一键,但解析器可以选择通过仅返回词法上最后一个重复的成员名称来处理此类 JSON,如 ECMAScript 5.1 的第 15.12 节(“JSON 对象”)中所指定的 @ 987654321@
标签: java json google-search-api