Created
January 14, 2021 19:30
-
-
Save itzg/71e5417aefe4c3ab2691afd4f26a4e36 to your computer and use it in GitHub Desktop.
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 com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.util.StringJoiner; | |
class TryReadTreeAndConvert { | |
static class Place { | |
String city; | |
String state; | |
public String getCity() { return city; } | |
public void setCity(String city) { this.city = city; } | |
public String getState() { return state; } | |
public void setState(String state) { this.state = state; } | |
public String toString() { | |
return new StringJoiner(", ", Place.class.getSimpleName() + "[", "]") | |
.add("city='" + city + "'") | |
.add("state='" + state + "'") | |
.toString(); | |
} | |
} | |
public static void main(String[] args) throws JsonProcessingException { | |
final ObjectMapper objectMapper = new ObjectMapper(); | |
final JsonNode tree = objectMapper.readTree("{\n" | |
+ " \"place\": {\n" | |
+ " \"city\": \"Dallas\",\n" | |
+ " \"state\": \"Texas\"\n" | |
+ " }\n" | |
+ "}"); | |
final Place place = (Place) objectMapper.convertValue(tree.get("place"), Place.class); | |
System.out.println(place); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs: