Jackson将JSON属性反序列化到不同的Java对象
我试图解析一个JSON结构,如下所示Jackson将JSON属性反序列化到不同的Java对象,java,json,jackson,json-deserialization,Java,Json,Jackson,Json Deserialization,我试图解析一个JSON结构,如下所示 { "service":{ "id":"7ba68ee3-73fc-4d0e-9819-6f3f23b97ab3", "state":"new", "operatingState":"Unknown", "serviceCharacteristic":[ { "name":"privateId", "value":[
{
"service":{
"id":"7ba68ee3-73fc-4d0e-9819-6f3f23b97ab3",
"state":"new",
"operatingState":"Unknown",
"serviceCharacteristic":[
{
"name":"privateId",
"value":[
{
"number":"505013486018300"
}
]
},
{
"name":"publicId",
"value":[
{
"serviceNumber":"34344141",
"accessRestriction": "true"
}
]
},
{
"name":"serviceProviderId",
"value":"0002"
},
{
"name":"type_w",
"value":[
{
"number":"505013486018300"
}
]
},
{
"name":"type_f",
"value":[
{
"number":"2323123",
"enabled": "true"
}
]
}
],
"supportingService":[
{
"id":"b28f4a28-4226-4976-9373-85c0bc13b440",
"state":"active",
"operatingState":"Unknown",
"serviceDate":"2019-12-20T12:05:58.000Z",
"serviceCharacteristic":[
{
"name":"profile",
"value":"default"
},
{
"name":"type_s",
"value":[
"bar-a",
"bar-b"
]
},
{
"name":"type_a",
"value":"false"
},
{
"name":"type_b",
"value":"cliBlk"
},
{
"name":"type_c",
"value":"true"
},
{
"name":"type_d",
"value":"false"
},
{
"name":"call_me",
"value":"true"
},
{
"name":"defaultCallBackNotification",
"value":"false"
}
]
}
]
}
}
搜索/读取后,按照提供的解决方案()编写了一个自定义JSON反序列化程序(StdDeserializer)。我可以在反序列化程序中获取JSON节点(值),但是
在映射和返回正确的Java对象时遇到问题
自定义反序列化程序的原因是与属性(值)关联的数据类型不同。客户端正在发送三种不同的类型
1. Created abstract class value
public abstract class Value {
public Value() {
}
}
2. Created a class to hold list of objects
@JsonIgnoreProperties(ignoreUnknown = true)
public class ListOfValueObjects extends Value implements Serializable
{
public List<ValueObject> value;
public ListOfValueObjects() {
super();
}
public static class ValueObject implements Serializable
{
@JsonProperty("enabled")
public String enabled;
@JsonProperty("accessRestriction")
public String accessRestriction;
@JsonProperty("serviceNumber")
public String serviceNumber;
@JsonProperty("number")
public String number;
public ValueObject() {
}
}
}
3. Created class for list of string
public class ValueList extends Value implements Serializable
{
public List<String> value;
public ValueList() {
super();
}
}
3. Created class for string value
public class ValueString extends Value implements Serializable
{
public String value;
public ValueString() {
super();
}
}
4. The top class looks like
public class service implements Serializable
{
// other properties
public List<ServiceCharacteristic> serviceCharacteristic = null;
@JsonProperty("supportingService")
@Valid
public List<SupportingService> supportingService = null;
}
public static class ServiceCharacteristic implements Serializable
{
@JsonProperty("name")
public String name;
@Valid
@JsonDeserialize(using = ValueDeserializer.class)
public Value value;
}
5. Dserializer look like ( referncing the answer given in here -- https://stackoverflow.com/a/12459070/2082536 )
public class ValueDeserializer extends StdDeserializer<Value>
{
public ValueDeserializer() {
super(Value.class);
}
@Override
public Value deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException
{
ObjectCodec codec = jsonParser.getCodec();
// this should be the value node ???
JsonNode node = codec.readTree(jsonParser);
// base on node type switch to correct class
// and using object mapper deserialize to class
}
}
1。创建抽象类值
公共抽象类价值{
公共价值(){
}
}
2.创建了一个类来保存对象列表
@JsonIgnoreProperties(ignoreUnknown=true)
公共类ListOfValueObjects扩展了可序列化的值实现
{
公共列表值;
ValueObjects公共列表(){
超级();
}
公共静态类ValueObject实现可序列化
{
@JsonProperty(“已启用”)
启用公共字符串;
@JsonProperty(“访问限制”)
公共字符串访问限制;
@JsonProperty(“服务编号”)
公共字符串服务编号;
@JsonProperty(“编号”)
公共字符串号;
公共估价对象(){
}
}
}
3.为字符串列表创建了类
公共类ValueList扩展值实现可序列化
{
公共列表值;
资产评估师(){
超级();
}
}
3.为字符串值创建了类
公共类ValueString扩展值实现可序列化
{
公共字符串值;
公共价值字符串(){
超级();
}
}
4.头等舱看起来像
公共类服务实现可序列化
{
//其他属性
公共列表serviceCharacteristic=null;
@JsonProperty(“支持服务”)
@有效的
public List supportingService=null;
}
公共静态类ServiceCharacteristic实现可序列化
{
@JsonProperty(“名称”)
公共字符串名称;
@有效的
@JsonDeserialize(使用=ValueDeserializer.class)
公共价值;
}
5.Dserializer看起来像(参考这里给出的答案--https://stackoverflow.com/a/12459070/2082536 )
公共类ValueDeserializer扩展StdDeserializer
{
公共价值反序列化程序(){
super(Value.class);
}
@凌驾
公共值反序列化(JsonParser、JsonParser、DeserializationContext、DeserializationContext)引发IOException、JsonProcessingException
{
ObjectCodec codec=jsonParser.getCodec();
//这应该是值节点???
JsonNode=codec.readTree(jsonParser);
//基于节点类型切换到正确的类
//并使用对象映射器对类进行反序列化
}
}
杰克逊版本:2-10.1我觉得你的问题不是很清楚。您能更明确地说明什么是yur输入和预期输出吗?很抱歉,我想知道如何反序列化具有怪癖属性(在上面的示例中是“值”)的JSON数据。