gpt4 book ai didi

java - 将 JSON 映射到 java POJO (jackson)

转载 作者:行者123 更新时间:2023-11-30 05:25:26 25 4
gpt4 key购买 nike

我使用JAVA jackson将JSON映射到JAVA pojo对象,我的JSON文件是:

[
[
{
"name": "tetragrammatonList_stocks_headerColumn_amountStoreOther",
"id": "amountStoreOther"
},
{
"name": "tetragrammatonList_stocks_headerColumn_article.articleSubGroup.name",
"id": "article.articleSubGroup.name"
},
.....
{
"name": "tetragrammatonList_stocks_headerColumn_article.producer.name",
"id": "article.producer.name"
}
],
[
{
"name": "tetragrammatonList_stocks_headerColumn_articleEANs",
"id": "articleEANs"
},
{
"name": "tetragrammatonList_stocks_headerColumn_article.plu",
"id": "article.plu"
},
{
"name": "tetragrammatonList_stocks_headerColumn_article.name",
"id": "article.name"
},
.....
{
"name": "tetragrammatonList_stocks_headerColumn_article.producer.name",
"id": "article.producer.name"
}
]
]

readValue 之后我收到此错误消息:

无法从 START_ARRAY token 中反序列化 .... 的实例

我的 POJO 类:

public class A
{
private String name;
private String id;
+get/set methods
}

public class B
{
private String name;
private String id;
+get/set methods
}

public class Root
{
private List<A> a;
private List<B> b;
+get/set methods
}

Root root = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(jsonString, Root.class);

请问,这种 JSON 格式的正确 java pojo 是什么?谢谢。

最佳答案

@Getter
@Setter
public class Root
{
private List<A> a;
}
@Getter
@Setter
private class A
{
private String name;
private String id;
}
ObjectMapper mapper = new ObjectMapper();
List <Root> rt = mapper.readValue(json, List.class);

试试这个

关于java - 将 JSON 映射到 java POJO (jackson),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58763665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com