Parsing a JSON Object - Java JSON

Java examples for JSON:JSON Parse

Description

Parsing a JSON Object

import java.io.InputStreamReader;
import java.io.Reader;

import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParser.Event;

public class Main {

  public void parseObject() {
    Reader fileReader = new InputStreamReader(getClass().getResourceAsStream(
        "BookObject.json"));
    JsonParser parser = Json.createParser(fileReader);

    while (parser.hasNext()) {
      Event ev = parser.next();
      System.out.println(ev);
      if (ev.equals(Event.VALUE_STRING)) {
        System.out.println(parser.getString());
      }
    }
  }

  public static void main(String[] args) {
    Main ex = new Main();
    ex.parseObject();
  }
}

createParser Method Input Types

Input Type Method Call
InputStream createParser(InputStream in)
JsonArray createParser(JsonArray arr)
JsonObject createParser(JsonObject obj)
Reader createParser(Reader reader)

JSON Object Events

Event Occurrence
START_OBJECT Start of an object.
END_OBJECTEnd of an object.
START_ARRAY Start of an array.
END_ARRAY End of an array.
KEY_NAME Name of a key.
VALUE_STRING Value of a name/value pair in String format.
VALUE_NUMBER Value of a name/value pair in numeric format.
VALUE_TRUEValue of a name/value pair in Boolean format.
VALUE_FALSE Value of a name/value pair in Boolean format.
VALUE_NULLValue of a name/value pair as NULL.

Related Tutorials