/ / Wie deserialisiere ich von Json Tree? - Java, Json, Jackson

Wie von Json Tree deserialisieren? - java, json, jackson

Angenommen, ich habe Json Tree bereits gelesen.

Ist es möglich, daraus zu deserialisieren (ohne zurück in einen String zu konvertieren)?

public class TryDeserializeNode {

public static class MyClass {

private int x = 11, y = 12;

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}
}

public static void main(String[] args) throws IOException {

ObjectMapper mapper = new ObjectMapper();

MyClass myClass = new MyClass();
String string = mapper.writeValueAsString(myClass);

JsonNode tree = mapper.readTree(string);

// how to deserialize from tree directly?
// MyClass myclass2 = mapper.readValue(tree.toString(), MyClass.class);
MyClass myclass2 = mapper.readValue(tree, MyClass.class);

}
}

Antworten:

5 für die Antwort № 1

Du könntest es einfach benutzen treeToValue:

MyClass myclass2 = mapper.treeToValue(tree, MyClass.class);

woher mapper ist dein Jackson Mapper und tree ist dein JsonNode.