Spring Boot中将JSON对象反序列化为抽象类
我有一个 JSON 对象,它有一个具有以下结构的键 (key1)
"key1": {
"key2": "some value1",
"key3": {
"key4": "some value2",
"key5": "some value3"
}
}
键 3 是动态的,因为它也可以具有以下数据
"key1": {
"key2": "some value1",
"key3": {
"key4": "some value2",
"key5": "some value3"
"key6": "some value4"
}
}
或
"key1": {
"key2": "some value1",
"key3": {
"key4": "some value2",
}
}
只有这 3 个组合是可能的(键 3 有 2 个属性、3 个属性或只有 1 个属性)。
考虑到所有这些,我在 Java 中为 key3 声明了一个抽象类(假设为 Key3 类),认为 key3 可以有多个实现(每个具体类都有自己的字段)。
现在有一个由类 Key3 的变量组成的 POJO(具有 Key3 的引用变量)
public Class MYPOJO class{
String variable1;
String variable2;
.
.
.
Key3 variable;
.
.
.
int variable3;
}
现在,当 MYPOJO 发生反序列化时,它会失败,因为 Key3 是抽象的,因此无法使用所需的具体类实例化(具体类将是三个类中的一个 - 具有 1、2 或 3 个字段)。具体的类可以根据 JSON 中 key2 的值或 key3 中的值来决定。
谁能建议我一些可以解决这个问题的方法,实例化一个具体类并将其传递给抽象类的引用变量?
或者有更好的解决方法来处理它?
I have a JSON object which has a key (key1) with the following structure
"key1": {
"key2": "some value1",
"key3": {
"key4": "some value2",
"key5": "some value3"
}
}
Key 3 is dynamic in that it can have the following data as well
"key1": {
"key2": "some value1",
"key3": {
"key4": "some value2",
"key5": "some value3"
"key6": "some value4"
}
}
or
"key1": {
"key2": "some value1",
"key3": {
"key4": "some value2",
}
}
Only these 3 combinations are possible (key 3 having 2 attributes, 3 attributes or just 1 attribute).
With all that in mind, I declared an abstract class (let's say Class Key3) for key3 in Java thinking that key3 can have multiple implementations (each concrete class will have their own fields).
Now there is a POJO composed of class Key3's variable (having a reference variable of Key3)
public Class MYPOJO class{
String variable1;
String variable2;
.
.
.
Key3 variable;
.
.
.
int variable3;
}
Now as the deserialization happens for MYPOJO, it fails as Key3 is abstract and hence can't be instantiated with a desired concrete class (concrete class will be either of the three classes - having 1, 2 or 3 fields). The concrete class can be decided on the basis of the value of key2 or whatever is present in key3 in JSON.
Can anyone suggest me something that can take care of this, instantiate a concrete class and pass it on to the reference variable of abstract class?
Or some better workaround to deal with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道您在哪种环境中要使用这种挑选方法,所以让我告诉您如何在Springboot中进行操作。
如果确定是key1,key2,key3,我可以使用以下方法接收。
控制台将结果打印为测试(key = aaa,key2 = some value1,key3 = {key4 = 16,key5 = jsf})。
证明可以通过使用MAP接收多个参数来完成避难所化,但是,此方法需要key1,key2和key3才能固定名称。
I don't know in what environment you want to use this deserialization method, so let me tell you how to do it in springboot.
If it is key1, key2, key3 is determined, I can use the following methods to receive.
The console prints the result as Test(key=aaa, key2=some value1, key3={key4=16, key5=jsf}).
It is proved that the deserialization can be completed by using map to receive multiple parameters, however, this method requires key1, key2, and key3 to be fixed names.
所以基本上你有两个选择。如果您知道总会有相同的结构,您可以创建用于响应的类并使用例如 GSON 进行反序列化。对象中总是有三个字段,JSON 响应中没有的字段为 null。第二个选项是在 Key3 类中创建字符串列表来存储这些值,但必须手动完成。
情况 1:
Key1 类
Key3 类
然后您可以使用以下代码反序列化 JSON 响应
请记住,如果 JSON 响应不包含(例如 key6),则该值将为 null。
案例2:
反序列化
So basically you have two options. If you know there'll always be the same structure you can create classes for response and deserialize with for example GSON. There are always three fields in object and the ones that are not in JSON response are null. The second option is to create list of strings in Key3 class to store those values but it has to be done manually.
Case 1:
Key1 class
Key3 class
Then you can deserialize JSON response with following code
Keep in mind if JSON response doesnt contain, for example key6, this value will be null.
Case 2:
Deserialization
您不需要带有3个可能实现的抽象类。只需创建1个类,其中
key3
属性是类型map< string,string>
的类型。您总是可以将其归为班级,并且地图中的班级可能会容纳1、2或3个键值对。您的班级可能如下:You don't need an abstract class with 3 possible implementations. Just create 1 class where
key3
property is of typeMap<String, String>
. You can always de-serialize it into your class and your class in your map may hold 1, 2 or 3 key-value pairs. Your class may look as follows: