如何使用Jackson来有条件地反序列化为POJO字段? [英] How to conditionally deserialize to a POJO field using Jackson?

查看:138
本文介绍了如何使用Jackson来有条件地反序列化为POJO字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何有条件地将JSON字符串反序列化为POJO字段?

How can I conditionally deserialize a JSON string to a POJO field?

我收到这样的JSON字符串:

I receive a JSON string like so:

{
 "status": "we stuck",
 "data" : "someData"
}

但是"someData"可以只是在桥下"的字符串,也可以是"[''bridge 5','Mandela bridge']"或"[{'incident 1':'['bridge 1' ,'bridge 2]'},{'incident 2':['bridge 99','还有其他']}]"

but "someData" can be just a string "under the bridge" or can be something like "['bridge 5', 'Mandela bridge']" or "[{'incident 1' : '['bridge 1', 'bridge 2]'},{'incident 2' : ['bridge 99', 'what ever else']}]"

如果数据"是一个数组,我想按原样返回json字符串,那么我将映射到处理该数组的其他类型

I want to return the json string AS IS if "data" is an array then I will map to a different Type that deals with the array

我有一个Java类:

class Response {
  String status;
  String data;
}

其他类型的数据将为

ArrayList<SomeOtherType> data;

这是我到目前为止所拥有的

This is what i have so far

 ObjectMapper mapper = new ObjectMapper();
 Response rspns = mapper.readValue(<theJSONStrHere>, Response.class);

当数据是数组时,这会失败,给我消息

this fails when data is an array, giving me the message

无法从START_ARRAY令牌中反序列化java.lang.String实例

can not deserialize instance of java.lang.String out of START_ARRAY token

我不知道从这里去哪里.

I don't know where to go from here.

推荐答案

您可以按照澳大利亚的说法使用自定义解串器,也可以只修改工作的解决方案.

You can either use a custom deserializer as aussie said or you can just modify your working solution.

class Response {
  String status;
  String data;
}

class Other {
  String status;    
  ArrayList<SomeOtherType> data;
}

ObjectMapper mapper = new ObjectMapper();
Other rspns = mapper.readValue(<theJSONStrHere>, Other.class);

这将使用ArrayList将JSON字符串解析为Other类. 现在该轮到您决定使用

This will parse the JSON String to the Other class with the ArrayList. Now it's your turn to implement the decision of then to use

Other rspns = mapper.readValue(<theJSONStrHere>, Other.class);

或何时使用

Responserspns = mapper.readValue(<theJSONStrHere>, Response.class);

注意:以上内容是一种快速而肮脏的解决方案.它的工作原理是这样的,但是我强烈建议您使用自定义解串器,该解串器处理的是什么以及返回什么的逻辑. 另外请记住,要使此方法最好地发挥作用,您可能会考虑将POJO结构构建到多个类中,这些类扩展了基类,然后可以通用.

Note: The above is a quick and dirty solution. It works like that but I would highly recommend to use a custom deserializer, which handles the logic of what it is and what to return. Also keep in mind that for this to work the best you might consider building the POJO structure to multiple classes which extend a base class and then work generic.

示例:

class response {
  String status;
}

class simpleResponse extends response {
  String data;
}

class listResponse extends response {
  ArrayList<Type> data;
}

class MyDeserializer extends JSONDeserializer<E extends response> {
   public E deserialize...) {

   }
}

要获取实际的工作示例,请阅读 Jackson

To get an actual working example read about Jackson

这篇关于如何使用Jackson来有条件地反序列化为POJO字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆