Java Reference
In-Depth Information
Once we are done adding properties to our JSON data, we need to invoke the
writeEnd() method of JsonGenerator , which adds the JSON end object character
(represented by a closing curly brace } ) to our JSON string.
At this point, the Writer or OutputStream we passed to Json.createGenerator()
contains a complete JSON object. What we do with it depends on our application
requirements; in our example we simply invoke the toString() method of the
StringWriter instance we used and assign its return value to the jsonStr variable.
Parsing JSON data with the JSON-P
streaming API
The following example illustrates how we can parse JSON data using the JSON-P
streaming API:
package com.ensode.jsonpstreamingapi;
//imports omitted
@Named
@RequestScoped
public class JsonPStreamingApiBean {
@Inject
private Person person;
private String jsonStr;
public String parseJson() {
StringReader stringReader = new StringReader(jsonStr);
JsonParser jsonParser = Json.createParser(stringReader);
Map<String, Object> jsonMap = new HashMap<>();
String jsonKeyNm = null;
Object jsonVal = null;
while (jsonParser.hasNext()) {
JsonParser.Event event = jsonParser.next();
if (event.equals(Event.KEY_NAME)) {
jsonKeyNm = jsonParser.getString();
} else if (event.equals(Event.VALUE_STRING)) {
jsonVal = jsonParser.getString();
 
Search WWH ::




Custom Search