ObjectNode

Extends JsonNode

The JsonObject class represents a JSON object node and provides methods for manipulating its data.

Methods

  • get(key: String): JsonNode

Retrieves the value of the specified property from the JSON object.


  • put(key: String, intValue: Integer): void

Adds or replaces a property with an integer value.


  • put(key: String, longValue: Long): void

Adds or replaces a property with a long value.


  • put(key: String, floatValue: Float): void

Adds or replaces a property with a float value.


  • put(key: String, doubleValue: Float): void

Adds or replaces a property with a double value.


  • put(key: String, textValue: String): void

Adds or replaces a property with a text value.


  • put(key: String, value: JsonNode): void

Adds or replaces a property with a JsonNode value.


  • get(key: String): JsonNode

Retrieves a property value, as JsonNode.


  • remove(key: String): void

Removes a property.


  • has(key: String): Boolean

Checks if a property exists.


  • size(): Integer

Retrieves the number of properties.


  • clear(): void

Removes all properties.


  • isEmpty(): Boolean

Checks if the object node is empty.


  • toString(): String

Returns the JSON node as a JSON string.

Examples

var objectNode = JsonUtil.createObjectNode();

// Add properties to the JSON object
objectNode.put("name", "John Doe");
objectNode.put("age", 30);
objectNode.put("isStudent", true);
objectNode.put("scores", 6.2);

// Retrieve a property value
var name = objectNode.get("name").toString();
var age = objectNode.get("age").asInt();
var isStudent = objectNode.get("isStudent").asBoolean();
var scores = objectNode.get("scores");

// Remove a property
objectNode.remove("age");

// Check if a property exists
var hasAge = objectNode.has("age");

// Get the number of properties
var numProperties = objectNode.size();

// Clear all properties
objectNode.clear();

// Check if the object is empty
var isEmpty = objectNode.isEmpty();

// Get the JSON representation
var jsonString = objectNode.toString();

Last updated