JSON-JEP

Full featured JSON Parser, Data Model, and JSON Path integration

This project is maintained by xmljim

JEP API Guide » Getting Started

Getting Started

Parsing JSON

To parse JSON, you need obtain a JSONParser instance from the JSONFactory:

JSONFactory factory = JSONFactory.newFactory() //instantiate the JSONFactory
JSONParser parser = factory.newParser() //

JSONParser Methods

The JSONParser interface includes several methods for loading a JSON instance:

In addition, you can also create an empty JSONObject or JSONArray instance:

Note: There is another way to create empty instances using, NodeFactory.newJSONObject() or NodeFactory.newJSONArray(), both of which are syntactic sugar for creating a JSONFactory, creating a new JSONParser instance, and invoking the parser’s new* method.

Example Read from an InputStream

JSONFactory factory = JSONFactory.newFactory();
JSONParser parser = factory.newParser();
JSONNode json = null; //JSONNode is the superinterface for both JSONObject and JSONArray

try (InputStream stream = ...) {
    json = parser.parse(stream);
} catch (IOException | JSONParserException e) {
    //do something in the event of an error
}

//we've obtained a JSONNode instance.  Now we can determine the type of Node and convert it:
if (json.isObject()) {
    JSONObject jsonObject = json.asJSONObject();
    //now we have access to all methods of the JSONObject
} else {
    JSONArray jsonArray = json.asJSONArray();
    //work with a JSONArray...
}

Sub-topics:

Next: Marshalling and Unmarshalling JSON and POJOs