我想使用Jettison解析JSON响应,使它们以XML格式保持一致。 到目前为止,我无法在解析部分得到任何进一步的信息。
我尝试申请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);
所以我想知道为什么输出是“Type2Type”而不是“Type2Value”以及我如何进入第二个?
答案 0 :(得分:1)
与XML一样,Jettison假设你的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