2013-12-08 12 views
1

JSON 응답을 구문 분석하여 XML 형식으로 일관되게 만들기 위해 Jettison을 사용하고 싶습니다. 지금까지는 구문 분석 부분에서 더 이상 얻을 수 없었습니다. Jettison으로 JSON 구문 분석

은 내가 출력이 "Type2Type"가 아니라 "Type2Value"왜 궁금하네요 그래서 this example

JSONObject obj = new JSONObject({"Type":2,"Value":{"rsp":true,"id":"5B721163"}}); 
AbstractXMLStreamReader reader = new MappedXMLStreamReader(obj); 

String s = reader.getName().getLocalPart(); 
String t = reader.getText(); 

reader.next(); 
String u = reader.getName().getLocalPart(); 
System.out.print(s+t+u); 

을 적용하기 위해 노력하고 나는 두 번째에 도착하는 방법? XML에서와 같이

답변

1

, 투하 가정하여 JSON은 루트 요소를 가지고 .next() 이벤트 유형은 다음과 같습니다 START_ELEMENT,END_ELEMENT,COMMENT...

JSONObject obj = new JSONObject(
     "{\"root\": {\"Type\":2,\"Value\":{\"rsp\":true,\"id\":\"5B721163\"}}}"); 
for (AbstractXMLStreamReader reader = new MappedXMLStreamReader(obj); reader 
     .hasNext(); reader.next()) { 
    String s = reader.getLocalName(); 
    String t = reader.getText(); 
    int evenType = reader.getEventType(); 
    System.out.println(String.format(
       "Name: %s, text: %s, event type: %d", s, t, evenType)); 
} 

나에게 줄 :

Name: root, text: null, event type: 7 (START_DOCUMENT) 
Name: root, text: null, event type: 1 (START_ELEMENT) 
Name: Type, text: 2, event type: 1 
Name: Type, text: 2, event type: 4 (CHARACTERS) 
Name: Type, text: null, event type: 2 (END_ELEMENT) 
Name: Value, text: null, event type: 1 
Name: rsp, text: true, event type: 1 
Name: rsp, text: true, event type: 4 
Name: rsp, text: null, event type: 2 
Name: id, text: 5B721163, event type: 1 
Name: id, text: 5B721163, event type: 4 
Name: id, text: null, event type: 2 
Name: Value, text: null, event type: 2 
Name: root, text: null, event type: 2 
+0

이유가없이 같은 방식으로 작동하지 않습니다 '루트'요소? –

+0

같은 이유로 다음 xml에서 root1 만 구문 분석합니다.

+0

StAXON에는 가상 루트 구성 옵션이 있습니다. http://java.dzone.com/articles/staxon-json-stax' XML에 단일 루트가 필요하므로 하지만 JSON 문서에는 JSON 형식이 없기 때문에 기존 JSON 형식을 읽고 쓰는 데 필요한 중요한 기능입니다. Jettison 프로젝트에서 해당 형식을 찾지 못했습니다. –