解决JSONException重复键 [英] resolve JSONException duplicate keys

查看:159
本文介绍了解决JSONException重复键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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..

我正在使用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?

这里是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多次出现

here il.secur.classif occurs multiple times

推荐答案

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

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.

如果在一个对象中有两个具有相同名称的键,JSONObject将引发异常。您可能希望更改对象,以便不在同一对象下重复键。可能会将昵称视为数组。

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 nickname as an array.

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

You need to paste the JSON object in the question.

这篇关于解决JSONException重复键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆