2011-02-06 4 views
3

은 JSON 결과는 중복 키를 가지고 있으며, 따라서 그것은 JSONException는 생산 .. 등 키 "별명을"중복해결 JSONException의 중복 키

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 여러 번

+1

'il.secur.classif','il.title','il.cutdate'는 다음과 같습니다. 되풀이했다. JSON이 잘못되었습니다! – Nishant

답변

7

JSON 개체 발생, 다른 객체처럼, 같은 이름을 가진 두 개의 속성을 가질 수 없습니다. 지도에서 같은 키를 두 번 사용하는 것과 같은 방식으로 불법입니다.

JSONObject는 한 객체에 이름이 같은 두 개의 키가 있으면 예외가 발생합니다. 동일한 객체 아래에서 키가 반복되지 않도록 객체를 변경하고자 할 수 있습니다. 아마도 nickname을 배열로 생각할 것입니다.

질문에 JSON 개체를 붙여 넣어야합니다.

0

이 기능이 정말로 필요한 경우 gson 1.6으로 되돌아갑니다. 해당 버전에는 중복 키가 허용됩니다.

0

Jackson 라이브러리를 사용하여 JSON을 구문 분석 할 수 있습니다. org.json의 패키지와 동일한 작업을하는 데 문제가 있었지만 Jackson으로 넘어 가서 해결했습니다 : http://wiki.fasterxml.com/JacksonHome

+0

링크가 끊어 졌습니까? –

+1

예, [this] (https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.9) 체크 아웃 – russellhoff