[英]Using Jackson to parse JSON and extract embedded type
Say I have a compound JSON object like so: 说我有一个像这样的复合JSON对象:
{
"Person": {
"name":"test",
"age": 20
},
"Animal": {
"name":"Max"
}
}
This JSON representation has two embedded types Person
and Animal
, yet, I want to parse and extract a representation of each individual type (resulting in two Strings?). 这个JSON表示具有两个嵌入式类型
Person
和Animal
,但是,我想解析并提取每个单独类型的表示(导致两个String?)。
Is this possible? 这可能吗? I was thinking of using Jackson but can't find a suitable example.
我当时在考虑使用杰克逊,但找不到合适的例子。
Any JSON parser can do this. 任何JSON解析器都可以做到这一点。
If you're not looking to map to a POJO and want to use Jackson, you're probably looking for the Tree Model: http://wiki.fasterxml.com/JacksonTreeModel 如果您不想映射到POJO并想使用Jackson,那么您可能正在寻找树模型: http : //wiki.fasterxml.com/JacksonTreeModel
It depends on exact details, but if you just mean that you have 2 different properties, with different types, you could have classes like: 这取决于确切的细节,但是如果您只是想拥有两个具有不同类型的不同属性,则可以使用类似以下的类:
public class Response {
public Person Person;
public Animal Animal;
}
public class Person {
public String name;
public int age;
}
public class Animal {
public String name;
}
(and/or use setters, getters). (和/或使用setter,getter)。
But if you are looking for polymorphic types (types Person and animal being related), it requires more work. 但是,如果您正在寻找多态类型(与人和动物相关的类型),则需要进行更多工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.