java 解决 JSONException 重复键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4912386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:36:04  来源:igfitidea点击:

resolve JSONException duplicate keys

javajsongoogle-search-api

提问by psy

i am using google custom search engine and getting the results in JSON format.for certain queries,the JSON result has duplicate keys and hence it produces a JSONException: Duplicate key "nickname" etc..

我正在使用谷歌自定义搜索引擎并以 JSON 格式获取结果。对于某些查询,JSON 结果具有重复的键,因此它会产生 JSONException: Duplicate key "nickname" 等。

i am using JAVA.

我正在使用 JAVA。

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

may know how to resolve this exception?

可能知道如何解决这个异常?

here is the JSON reply:

这是 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"
     }
    ]
   }
  }

here il.secur.classif occurs multiple times

这里 il.secur.classif 出现多次

回答by Nishant

JSon object, like any other object, can not have two attribute with same name. That's illegal in the same way as having same key twice in a map.

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

JSONObject would throw an exception if you have two keys with same name in one object. You may want to alter your object so that keys are not repeated under same object. Probably consider nicknameas an array.

如果一个对象中有两个同名的键,JSONObject 将抛出异常。您可能想要更改您的对象,以便键不会在同一对象下重复。可能认为nickname是一个数组。

You need to paste the JSON object in the question.

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

回答by Ted Bigham

If you really need this functionality, roll back to gson 1.6. Duplicate keys are allowed in that version.

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

回答by russellhoff

You can make use of the Hymanson library to parse JSON. I'd problems doing the same task as you with org.json's package, but I turned to Hymanson and I solved it: http://wiki.fasterxml.com/HymansonHome

您可以使用 Hymanson 库来解析 JSON。我在使用 org.json 的包执行与您相同的任务时遇到问题,但我求助于 Hymanson 并解决了它:http://wiki.fasterxml.com/HymansonHome