1

我需要用 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
    }
  ]
}
4

1 回答 1

1

我更熟悉 Gson,但我认为这应该可行。

请参阅下面的 StaxMan 注释:

Jackson 不会自动检测私有字段(可以使用 @JsonAutoDetect 或全局配置)。所以字段应该是私有的,用@JsonProperty 注释,或者有匹配的公共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;
    }
}
于 2012-11-27T22:51:20.320 回答