resolve JSONException duplicate keys
Asked Answered
R

3

4

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

Raneeraney answered 6/2, 2011 at 8:39 Comment(2)
il.secur.classif, il.title, il.cutdate are repeated. The JSON is wrong!Jaclynjaco
It's true that JSON requires unique keys, but parsers can choose to handle such JSON by returning only the lexically last duplicate member name, as specified in Section 15.12 ("The JSON Object") of ECMAScript 5.1 ecma-international.org/ecma-262/5.1/ECMA-262.pdfLionel
J
7

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.

Jaclynjaco answered 6/2, 2011 at 8:59 Comment(0)
C
1

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

Carapace answered 11/10, 2012 at 17:59 Comment(1)
From experience, some unsuspecting person is going to upgrade the library and see no compilation errors. So put a prominent comment in your pom.xml, build.gradle etc. telling them it's on 1.6 for a reason. Maybe even add a unit test.Logsdon
G
0

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

Gliadin answered 4/12, 2014 at 12:42 Comment(2)
Link is broken?Circumbendibus
Yes it is, check this outGliadin

© 2022 - 2024 — McMap. All rights reserved.