英文:
Unable to convert JSON Array into a List in Java
问题
你可以更改JSON的结构,以便使用Jackson正确地接收带有嵌套子条件的条件列表。下面是更改后的JSON:
{
"conditions": [
{
"type": "NONE",
"subConditions": []
},
{
"type": "AND",
"subConditions": [
{
"type": "NONE",
"subConditions": []
}
]
},
{
"type": "OR",
"subConditions": []
}
]
}
将JSON对象的键从"condition"更改为"conditions",以便更好地反映您的数据结构。现在,您可以使用Jackson将其反序列化为具有适当嵌套子条件的条件列表。
在Java代码中,您的类和方法保持不变,只需更改JSON输入的格式。这应该让Jackson正确解析JSON并创建带有嵌套子条件的条件列表。
英文:
I have an issue in converting JSONArray into a List using Jackson Object Mapper.
public class Main {
public static void main(String[] args) throws Exception {
String test = Files.readString(Path.of("test.json"));
callRequired(test);
}
public static void callRequired(String test) {
ObjectMapper mapper = new ObjectMapper();
try {
List<Condition> result = mapper.readerForListOf(Condition.class).readValue(test.trim());
System.out.println(result);
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
The test.json is:
{
"condition": [
{
"type": "NONE",
"subConditons": [
{
"type": "NONE",
"subConditions": []
},
{
"type": "AND",
"subConditions": [
{
"type": "NONE",
"subConditions": []
}
]
}
]
},
{
"type": "OR",
"subConditions": []
}
]
}
The Condition class is as follows:
public class Condition {
Type type = Type.NONE;
List<Condition> subConditions;
public Condition() {
}
public Condition(Type type, List<Condition> subConditions) {
this.type = type;
this.subConditions = subConditions;
}
public List<Condition> getSubConditions() {
return subConditions;
}
public void setSubConditions(List<Condition> subConditions) {
this.subConditions = subConditions;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public enum Type {
NONE,
AND,
OR;
}
@Override
public String toString() {
return "Condition [type=" + type + ", subConditions=" + subConditions + "]";
}
}
Edit: The Condition array contains a list of conditions within which lies a subCondition which is also a type of Condition.
EDIT: Thanks, I have added the requisite braces and checked for its validity. I have also added an all args construcutor and a no args constructor as well.
How do I change the JSON so that I receive a List of conditions with the proper subconditions nested with Jackson.
答案1
得分: 1
在你的JSON中,"condition"对象以"["开始,将其类型设置为数组。这不是你的条件对象类似的样子。
应该更像是:
[
{
"type" : "NONE",
"subConditions" : []
}
]
英文:
In your JSON, the "condition" object starts with a "[", setting its type to an array. This isn't how your condition object class look like.
It should be more like:
[
{
"type" : "NONE",
"subConditions" : []
}
]
答案2
得分: 0
以下是翻译好的代码部分:
String test = Files.readString(Path.of("test.json"));
Gson gson = new Gson();
JsonElement element = gson.fromJson(test, JsonObject.class).get("condition");
List<Condition> conditions = Arrays.asList(gson.fromJson(element, Condition[].class));
class Condition {
Type type;
List<Condition> subConditions;
@Override
public String toString() {
return "(%s, %s)".formatted(type, subConditions);
}
}
enum Type {
NONE, AND, OR
}
Output
(NONE, [(NONE, []), (AND, [(NONE, [])])])
(OR, [])
英文:
If you have no objection to switching to Gson, here is a basic parse.
String test = Files.readString(Path.of("test.json"));
Gson gson = new Gson();
JsonElement element = gson.fromJson(test, JsonObject.class).get("condition");
List<Condition> conditions = Arrays.asList(gson.fromJson(element, Condition[].class));
class Condition {
Type type;
List<Condition> subConditions;
@Override
public String toString() {
return "(%s, %s)".formatted(type, subConditions);
}
}
enum Type {
NONE, AND, OR
}
Output
(NONE, [(NONE, []), (AND, [(NONE, [])])])
(OR, [])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论