Skip to content

Instantly share code, notes, and snippets.

@evaldeslacasa
Created June 8, 2015 14:56
Show Gist options
  • Save evaldeslacasa/4457b1382bdb8aecfb9d to your computer and use it in GitHub Desktop.
Save evaldeslacasa/4457b1382bdb8aecfb9d to your computer and use it in GitHub Desktop.
Java class with method to get node value from JSON String using Jackson
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
/**
* The readNodeValue method allows us to get a node value
* using the JSON Jackson library in Java. In this case
* we are interested in getting the value of a child node
* nested inside of a father node.
*
* The JSON input would be like this:
* {FatherNode: { ChildNode:(value),...}}
*
* @author Enrique Valdes
**/
public class ReadJsonNode {
public static String readNodeValue(String jsonInput){
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode;
String value = "";
try {
jsonNode = objectMapper.readTree(jsonInput);
jsonNode = jsonNode.get("FatherNode");
value = jsonNode.get("ChildNode").getTextValue();
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment