Interface JsonNode
- All Known Subinterfaces:
JsonArrayNode,JsonBooleanNode,JsonNullNode,JsonNumberNode,JsonObjectNode,JsonStringNode
This interface is used to represent a JSON node, and acts as the base for all other JsonNode types (see subinterfaces).
You can create a JsonNode from a raw JSON string using jsonNode(String), which will attempt to parse the JSON into a specific JsonNode type.
To retrieve the created JsonNode as a JsonArray, or a JsonObject, call asArray() or asObject() respectively. This provides additional utility for the specific JSON type.
Note: If you are parsing a JsonObject, you can use JsonObjectNode and its utility methods. Call asObject() on your JsonNode.
Note: Attempting to retrieve the JsonNode as the wrong type will throw an IllegalStateException.
JsonNode jsonNode = JsonNode.create("[]"); // The string parsed, and the underlying type will be JsonArrayNode.
JsonObjectNode objectNode = jsonNode.asObject(); // Throws IllegalStateException, as the JSON string was parsed as a JsonArrayNode.
JsonArrayNode arrayNode = jsonNode.asArray(); // Successful
Each specific JsonNode type has a corresponding factory method:
// Create a JsonStringNode with the value "foo"
JsonStringNode stringNode = JsonStringNode.jsonStringNode("foo");
// Create a JsonNumberNode with the value 2.5
JsonNumberNode numberNode = JsonNumberNode.jsonNumberNode(2.5);
// Create a JsonArrayNode containing the two above nodes
JsonArrayNode arrayNode = JsonArrayNode.jsonArrayNode(
stringNode,
numberNode
);
Any JsonNode can return its JSON string representation:
JsonArrayNode arrayNode = JsonArrayNode.jsonArrayNode(
stringNode,
numberNode
);
String arrayNodeAsJson = arrayNode.toJsonString();
System.out.println(arrayNodeAsJson);
Prints:
[
"foo",
2.5
]
-
Method Summary
Modifier and TypeMethodDescriptionasArray()Attempts to return thisJsonNodeas a list of nodes.Attempts to return thisJsonNodeas a boolean.asDouble()Attempts to return thisJsonNodeas a double.asLong()Attempts to return thisJsonNodeas a long.asNumber()Attempts to return thisJsonNodeas a number.asObject()Attempts to return thisJsonNodeas an object.asString()Attempts to return thisJsonNodeas a string.getValue()Retrieves the value for thisJsonNode.booleanisArray()Checks if thisJsonNodeis an array.booleanChecks if thisJsonNodeis a boolean.booleanisNull()Checks if thisJsonNodeis null.booleanisNumber()Checks if thisJsonNodeis a number.booleanisObject()Checks if thisJsonNodeis an object.booleanisString()Checks if thisJsonNodeis a string.static JsonNodeCreates a new instance ofJsonNodefrom the supplied json string.Returns thisJsonNodeas its string representation.
-
Method Details
-
getValue
-
toJsonString
-
isArray
boolean isArray()Checks if thisJsonNodeis an array.- Returns:
- True if this JsonNode represents a JSON array.
-
isObject
boolean isObject()Checks if thisJsonNodeis an object.- Returns:
- True if this JsonNode represents a JSON object.
-
isString
boolean isString()Checks if thisJsonNodeis a string.- Returns:
- True if this JsonNode represents a JSON string.
-
isNumber
boolean isNumber()Checks if thisJsonNodeis a number.- Returns:
- True if this JsonNode represents a JSON number.
-
isBoolean
boolean isBoolean()Checks if thisJsonNodeis a boolean.- Returns:
- True if this JsonNode represents a JSON boolean.
-
isNull
boolean isNull()Checks if thisJsonNodeis null.- Returns:
- True if this JsonNode represents a null value.
-
asBoolean
Boolean asBoolean()Attempts to return thisJsonNodeas a boolean.- Throws:
IllegalStateException- If this JsonNode is not a boolean type.
-
asString
String asString()Attempts to return thisJsonNodeas a string.- Throws:
IllegalStateException- If this JsonNode is not a string type.
-
asNumber
Number asNumber()Attempts to return thisJsonNodeas a number.- Throws:
IllegalStateException- If this JsonNode is not a number type.
-
asLong
Long asLong()Attempts to return thisJsonNodeas a long.- Throws:
IllegalStateException- If this JsonNode is not a number type.
-
asDouble
Double asDouble()Attempts to return thisJsonNodeas a double.- Throws:
IllegalStateException- If this JsonNode is not a number type.
-
asArray
JsonArrayNode asArray()Attempts to return thisJsonNodeas a list of nodes.- Throws:
IllegalStateException- If this JsonNode is not an array type.
-
asObject
JsonObjectNode asObject()Attempts to return thisJsonNodeas an object.- Throws:
IllegalStateException- If this JsonNode is not an object type.
-
jsonNode
Creates a new instance ofJsonNodefrom the supplied json string.- Parameters:
json- The JSON string, which may use single quotes in place of double quotes.- Returns:
- A new
JsonNodeinstance. - Throws:
JsonParseException- If the string is not valid JSON.
-