gpt4 book ai didi

java - 使用 Jackson 解析复杂的 JSON 对象

转载 作者:行者123 更新时间:2023-11-30 03:54:18 24 4
gpt4 key购买 nike

我需要使用 Jackson 库解析相对复杂的 JSON。你们能建议我应该拥有什么样的 Java 类结构以及使用什么 Jackson 方法来解析以下 JSON 对象。

是这样的:

{
"firstField": "Something One",
"secondField": "Something Two",
"thirdField": [
{
"thirdField_one": "Something Four",
"thirdField_two": "Something Five"
},
{
"thirdField_one": "Something Six",
"thirdField_two": "Something Seven"
}
],
"fifthField": [
{
"fifthField_one": "Something… ",
"fifthField_two": "Something...",
"fifthField_three": 12345
},
{
"fifthField_one": "Something",
"fifthField_two": "Something",
"fifthField_three": 12345
}
]
}

最佳答案

我更精通 Gson,但我认为这应该可行。

请参阅下面 StaxMan 的注释:

Jackson does not automatically detect private fields (it can be configured to, with @JsonAutoDetect or globally). So fields should either be private, annotated with @JsonProperty, or have matching public getter.

public class MyClass {
private String firstField, secondField;
private ThirdField thirdField;
private FifthField fifthField;

public static class ThirdField {
private List<ThirdFieldItem> thirdField;
}

public static class ThirdFieldItem {
private String thirdField_one, thirdField_two;
}

public static class FifthField {
private List<FifthFieldItem> fifthField;
}

public static class FifthField {
private String fifthField_one, fifthField_two;
private int fifthField_three;
}
}

关于java - 使用 Jackson 解析复杂的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13591597/

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