Jackson - Working with JSON in Java
Jackson is a set of tools for working with JSON data in Java. Jackson contains a wealth of features, so don’t expect this article to cover them all.
Parsing arbitrary JSON strings
To get started with Jackson, let’s look at how we can parse an arbitrary JSON string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package example;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Exampler {
public static void main(String args[]) throws Exception {
String json = "{\"hello\":\"world\"}";
JsonNode root = new ObjectMapper().readTree(json);
if (root.has("hello")) {
System.out.println("Value of hello is: " + root.get("hello"));
}
}
}
The example above prints:
1
Value of hello is: "world"