Created
June 17, 2024 13:04
-
-
Save glowinthedark/969277020b7793fca9fd9fe5141a900f to your computer and use it in GitHub Desktop.
JsonPointer example
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
package com.legbehindneck; | |
import java.io.IOException; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.core.JsonPointer; | |
public class Main { | |
public static void main(String[] args) { | |
ObjectMapper mapper = new ObjectMapper(); | |
String json = "{\"name\":{\"first\":\"John\",\"last\":\"Doe\"},\"age\":30,\"city\":\"New York\"}"; | |
JsonNode node = null; | |
try { | |
node = mapper.readTree(json); | |
JsonPointer pointer = JsonPointer.compile("/name/first"); | |
JsonNode firstNameNode = node.at(pointer); | |
System.out.println("First Name: " + firstNameNode.asText()); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment