JSON is a text-based data exchange format derived from JavaScript that is used in web services and other connected applications. The following sections provide an introduction to JSON syntax, an overview of JSON uses, and a description of the most common approaches to generate and parse JSON.
JSON Syntax
JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values. JSON defines six data types: string, number, object, array, true, false and null.
The following example shows JSON data for a sample object that contains name-value pairs. The value for the name
"phoneNumbers"
is an array whose elements are two objects.{ "firstName": "Duke", "lastName": "Java", "age": 18, "streetAddress": "100 Internet Dr", "city": "JavaTown", "state": "JA", "postalCode": "12345", "phoneNumbers": [ { "Mobile": "111-111-1111" }, { "Home": "222-222-2222" } ] }
Class or Interface:
Main Classes and Interfaces in javax.json
Class or Interface | Description |
---|---|
Json | Contains static methods to create instances of JSON parsers, builders, generators. This class also contains methods to create parser, builder, and generator factory objects. |
JsonReader | Reads JSON data from a stream and creates an object model in memory. |
JsonObjectBuilder JsonArrayBuilder | Create an object model or an array model in memory by adding elements from application code. |
JsonWriter | Writes an object model from memory to a stream. |
JsonValue | Represents an element (such as an object, an array, or a value) in JSON data. |
JsonStructure | Represents an object or an array in JSON data. This interface is a subtype of JsonValue . |
JsonObject JsonArray | Represent an object or an array in JSON data. These two interfaces are subtypes of JsonStructure . |
JsonString JsonNumber | Represent data types for elements in JSON data. These two interfaces are subtypes of JsonValue . |
JsonException | Indicates that a problem occurred during JSON processing. |
Classes and Interfaces in javax.json.stream
Class or Interface | Description |
---|---|
JsonParser | Represents an event-based parser that can read JSON data from a stream or from an object model. |
JsonGenerator | Writes JSON data to a stream one element at a time. |
Example:
import javax.json.Json; import javax.json.JsonObject; ... JsonObject model = Json.createObjectBuilder() .add("firstName", "Duke") .add("lastName", "Java") .add("age", 18) .add("streetAddress", "100 Internet Dr") .add("city", "JavaTown") .add("state", "JA") .add("postalCode", "12345") .add("phoneNumbers", Json.createArrayBuilder() .add(Json.createObjectBuilder() .add("type", "mobile") .add("number", "111-111-1111")) .add(Json.createObjectBuilder() .add("type", "home") .add("number", "222-222-2222"))) .build();
Functions of JsonObject
JsonObjectBuilder add(String name, BigDecimal value) JsonObjectBuilder add(String name, BigInteger value) JsonObjectBuilder add(String name, boolean value) JsonObjectBuilder add(String name, double value) JsonObjectBuilder add(String name, int value) JsonObjectBuilder add(String name, JsonArrayBuilder builder) JsonObjectBuilder add(String name, JsonObjectBuilder builder) JsonObjectBuilder add(String name, JsonValue value) JsonObjectBuilder add(String name, long value) JsonObjectBuilder add(String name, String value) JsonObjectBuilder addNull(String name)
FileWriter writer = new FileWriter("test.txt"); JsonGenerator gen = Json.createGenerator(writer); gen.writeStartObject() .write("firstName", "Duke")
JSON in Java EE RESTful Web Services
What Are RESTful Web Services?
JavaEE tutorial said that "Representational State Transfer (REST) is an architectural style of client-server application centered around the transfer of representations of resources through requests and responses"We have to have in mind the following concepts:Uniform interfaceURIHTTP METHODS POST,GET,DELETE,PUTSelf-descriptive messages HTML,XHTML,JSON,XML,ImagesStateful interactions through links
Summary of JAX-RS Annotations (by javaEE tutorial)
Tutorials:
No comments:
Post a Comment