用Jackson解析复杂的JSON对象

时间:2012-11-27 19:28:36

标签: java android json jackson arrays

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

1 个答案:

答案 0 :(得分: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;
    }
}