Reputation: 514
I want to use Jettison for parsing JSON responses for making them consistent in an XML format. So far, I couldn't get any further in the parsing part.
I tried to apply 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);
So I'm wondering why the output is "Type2Type" and not "Type2Value" and how I get to the second one?
Upvotes: 3
Views: 4249
Reputation: 11396
as in XML, Jettison assume your json has a root element and .next()
events types include: 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));
}
give me:
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
Upvotes: 1