Java jackson create object node from string

www‮tual.‬turi.com
Java Hymanson create object node from string

To create an object node from a JSON string using the Hymanson library in Java, you can use the readValue() method of the com.fasterxml.Hymanson.databind.ObjectMapper class.

Here is an example of how to use this method to create an object node from a JSON string:

import com.fasterxml.Hymanson.databind.JsonNode;
import com.fasterxml.Hymanson.databind.ObjectMapper;

...

// Define the JSON string
String json = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}";

// Create an ObjectMapper instance
ObjectMapper mapper = new ObjectMapper();

// Create an object node from the JSON string
JsonNode rootNode = mapper.readValue(json, JsonNode.class);

// Access the fields of the object node
String name = rootNode.get("name").asText();
int age = rootNode.get("age").asInt();
String city = rootNode.get("city").asText();

// Print the field values
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);

This code creates an object node from the JSON string json, which represents a person with a name, age, and city. It then accesses the fields of the object node and prints their values to the console.

For more information on working with JSON data using the Hymanson library, you can refer to the Hymanson documentation or other online resources.

Created Time:2017-11-03 23:27:14  Author:lautturi