开发者

resolve JSONException duplicate keys

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

i am using 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:

{
   "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


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

You need to paste the JSON object in the question.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜