Created
June 8, 2015 14:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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