public final class JSON extends Object
To parse a given JSON input, use the parse()
methods like in this example:
JsonObject object = Json.parse(string).asObject();
To create a JSON data structure to be serialized, use the
methods value(), array(), and object()
. For example, the following snippet will produce the JSON string
{"foo": 23, "bar": true}:
String string = Json.object().add("foo", 23).add("bar", true).toString();
To create a JSON array from a given Java array, you can use one of the
array() methods with varargs parameters:
String[] names = ... JsonArray array = Json.array(names);
| 限定符和类型 | 字段和说明 |
|---|---|
static JSONValue |
FALSE
Represents the JSON literal
false. |
static JSONValue |
NULL
Represents the JSON literal
null. |
static JSONValue |
TRUE
Represents the JSON literal
true. |
| 限定符和类型 | 方法和说明 |
|---|---|
static JSONValue |
array()
Creates a new empty JsonArray.
|
static JSONArray |
array(Boolean... values)
Creates a new JsonArray that contains the JSON representations of the
given
boolean values. |
static JSONArray |
array(Double... values)
Creates a new JsonArray that contains the JSON representations of the
given
double values. |
static JSONArray |
array(Float... values)
Creates a new JsonArray that contains the JSON representations of the
given
float values. |
static JSONArray |
array(Integer... values)
Creates a new JsonArray that contains the JSON representations of the
given
int values. |
static JSONArray |
array(Long... values)
Creates a new JsonArray that contains the JSON representations of the
given
long values. |
static JSONArray |
array(String... strings)
Creates a new JsonArray that contains the JSON representations of the
given strings.
|
static JSONObject |
create()
Creates a new empty JsonObject.
|
static JSONValue |
parse(Reader reader)
Reads the entire input from the given reader and parses it as JSON.
|
static JSONValue |
parse(String string)
Parses the given input string as JSON.
|
static JSONValue |
value(Boolean value)
Returns a JsonValue instance that represents the given
boolean value. |
static JSONValue |
value(Byte value) |
static JSONValue |
value(Double value)
Returns a JsonValue instance that represents the given
double value. |
static JSONValue |
value(Float value)
Returns a JsonValue instance that represents the given
float
value. |
static JSONValue |
value(Integer value)
Returns a JsonValue instance that represents the given
int
value. |
static JSONValue |
value(Long value)
Returns a JsonValue instance that represents the given
long
value. |
static JSONValue |
value(Object value) |
static JSONValue |
value(String string)
Returns a JsonValue instance that represents the given string.
|
public static final JSONValue NULL
null.public static final JSONValue TRUE
true.public static final JSONValue FALSE
false.public static JSONValue value(Integer value)
int
value.value - the value to get a JSON representation forpublic static JSONValue value(Long value)
long
value.value - the value to get a JSON representation forpublic static JSONValue value(Float value)
float
value.value - the value to get a JSON representation forpublic static JSONValue value(Double value)
double value.value - the value to get a JSON representation forpublic static JSONValue value(String string)
string - the string to get a JSON representation forpublic static JSONValue value(Boolean value)
boolean value.value - the value to get a JSON representation forpublic static JSONValue array()
public static JSONArray array(Integer... values)
int values.values - the values to be included in the new JSON arraypublic static JSONArray array(Long... values)
long values.values - the values to be included in the new JSON arraypublic static JSONArray array(Float... values)
float values.values - the values to be included in the new JSON arraypublic static JSONArray array(Double... values)
double values.values - the values to be included in the new JSON arraypublic static JSONArray array(Boolean... values)
boolean values.values - the values to be included in the new JSON arraypublic static JSONArray array(String... strings)
strings - the strings to be included in the new JSON arraypublic static JSONObject create()
public static JSONValue parse(String string)
string - the input string, must be valid JSONParseException - if the input is not valid JSONpublic static JSONValue parse(Reader reader) throws IOException
Characters are read in chunks into an input buffer. Hence, wrapping a
reader in an additional BufferedReader likely won't improve
reading performance.
reader - the reader to read the JSON value fromIOException - if an I/O error occurs in the readerParseException - if the input is not valid JSONCopyright © 2016. All rights reserved.