Java examples for JSON:JSON Parse
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_OBJECT | End 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_TRUE | Value of a name/value pair in Boolean format. |
VALUE_FALSE | Value of a name/value pair in Boolean format. |
VALUE_NULL | Value of a name/value pair as NULL. |