Java Magazine, July/August 2016
ORACLE COM JAVAMAGAZINE JULY AUGUST 2016 34 enterprise java To use this example just invoke the method and pass it a JSON object navigate country null The object model API also permits via the JsonWriter class outputting a JSON object or array to a stream You first use the Json createWriter method to specify the output stream to use The JsonWriter writeObject method then writes the JSON object to that stream Finally you need to close the output stream either by calling the JsonWriter close method or via the AutoCloseable try with resources approach as illustrated in the example below StringWriter strWriter new StringWriter try JsonWriter jsonWriter Json createWriter strWriter jsonWriter writeObject country The JSON P Streaming API The second JSON P API is a lower level streaming API that is conceptually similar to StAX This streaming API provides forward only read only access to JSON data in a streaming way It is particularly well suited for reading in an eficient manner large JSON payloads The streaming API also allows you to write JSON data to output in a streaming fashion This API resides in the javax json stream package The JsonParser interface is at the core of this streaming API It provides forward only read only access to JSON data using a pull parsing programming model In this pull model the application controls the parser by repeatedly calling JsonParser methods to advance the parser Based on that the parser state will change and parser events will be generated to reflect this The pull parser can generate any of the following JSON P obviously is not the first Java based JSON related API but it is the first one that has been standardized through the Java Community Process self explanatory events START_ OBJECT END_ OBJECT START_ ARRAY END_ ARRAY KEY_ NAME VALUE_ STRING VALUE_ NUMBER VALUE_ TRUE VALUE_ FALSE and VALUE_ NULL The application logic should leverage these different events to advance the parser to the necessary position within the JSON document to obtain the required data First create a pull parser using the Json createParser method from either an InputStream or a Reader The application will then keep advancing the parser forward by calling the hasNext method Has the parser reached the end yet and next method on the parser Keep in mind that the parser can be moved in only one direction forward The following example uses a free online service that exposes country related information in JSON The code is simply creating a streaming parser from an inputStream using the Json createParser method The application then keeps advancing the parser to go over each country In this case the parsing logic is looking at only two keys name and capital For each country the application looks at the name value if it is not France the application keeps advancing the parser Once France is found the application looks only at the capital key Because the current parser state is Event KEY_ NAME that is the parser is on Frances capital key the application advances the parser one step Event VALUE_ STRING and gets the actual value of the capital using the getString method on the parser Once this is done it is useless to continue parsing the rest of the JSON stream so the application exits the loop
You must have JavaScript enabled to view digital editions.