Parse JSON with Jackson

August 17, 2017

I wanted to parse a small JSON response message in Java, using the Jackson library - version 2.4.5.

The code is as follows, where pJsonResponse is the passed in JSON to parse:

ObjectMapper lMapper = new ObjectMapper();
JsonNode lParsedValue;
try
{
  lParsedValue = lMapper.readTree(pJsonResponse);
}
catch (IOException lException)
{
  throw new RuntimeException("Failed to parse json response", lException);
}

JsonNode lRootValue = lParsedValue.get("email");

this.value = lRootValue.get("value").asText();
this.result = lRootValue.get("result").asText();
this.timing = lRootValue.get("timing").asInt();

The JSON being parsed is:

{
  "email"   :
   {
    "value" : "test@example.com",
    "result" : "valid",
    "timing" : 40
    }
 }

The libraries I needed to make this work were:

  • jackson-core.jar
  • jackson-databind.jar
  • jackson-annotations.jar

Tags: json java jackson