Monday, July 14, 2014

Low-Level JSON Parsing With Jackson

Jackson is primary used for mapping Java classes to JSON objects. However, there are times where you need to retrieve the raw text content of a specific JSON node rather than to map the entire document to a class.

This can be done with the following code:

ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getJsonFactory();
JsonParser jp = factory.createJsonParser(jsonString);
JsonNode node = mapper.readTree(jp);
JsonNode node1 = node.get(“key1”);
JsonNode node2 = node2.get("key2");
String node2Content = node2.asText();

References:

No comments:

Post a Comment