Json使用jackson进行POJO解析 - @JsonPropertyOrder用于以下Json根据#students而变化?

时间:2014-10-09 22:47:02

标签: java json jackson pojo

这是我的json输入

{
  "students_key": {
    "student_key_one": {
      "profile_root": "/profile/student_key_one/",
      "nickname": "sam1",
      "email": "sam1@gmail.com",
      "studentkey": "student_key_one"
    },
    "student_key_two": {
      "profile_root": "/profile/student_key_two/",
      "nickname": "sam2",
      "email": "sam2@gmail.com",
      "studentkey": "student_key_two"
    },
    "student_key_three": {
      "profile_root": "/profile/student_key_three/",
      "nickname": "sam3",
      "email": "sam3@gmail.com",
      "studentkey": "student_key_three"
    },
    "student_key_four": {
      "profile_root": "/profile/student_key_four/",
      "nickname": "sam4",
      "email": "sam4@gmail.com",
      "studentkey": "student_key_four"
    },
    "student_key_five": {
      "profile_root": "/profile/student_key_five/",
      "nickname": "sam5",
      "email": "sam5@gmail.com",
      "studentkey": "student_key_five"
    }
  }
}

随着学生人数的增加,属性也会增加(student_key_one,student_key_two,student_key_three,student_key_four,student_key_five,student_key_six .......) 该属性将根据#students动态增加或减少。

由于这不是一个对象数组,我如何为StudentsKey创建POJO? 有人可以帮助我使用jackson编写用于反序列化的pojo吗?

1 个答案:

答案 0 :(得分:1)

打破它:

外包装器是一个对象,应该是它自己的类

+--------------------------+
|                          |
|  {                       |
|      "students_key": {   |
|           ...            |
|      }                   |
|  }                       |
|                          |       
+--------------------------+

{ }花括号表示一个对象,在映射术语中表示一个类。该类只有一个属性students_key。因此,我们可以使用一个字段 Students

创建一个类students_key
public class Students {
    @JsonProperty("students_key")
    (???????) students;
}

students_key应该使用哪种类型?让我们来看看结构

"students_key": {
    "student_key_one": {
       ...
    }
    "student_key_two": {
       ...
    }
    ...
 }

我们应该问自己哪种数据结构最能支持键/值的概念。首先想到的是Map。因此,如果我们将students_key设为Map,则类型将为

 //       (key)          (value)
 Map <    String     ,   Object    >
 //   "student_key_one": { ... }  

我们可以更进一步,给Object一个实体类型,因为我们有更多的属性。所以我们可以创建一个类Student

public class Student {
    @JsonProperty("profile_root")
    private String profileRoot;
    @JsonProperty("nickname")
    private String nickname;
    @JsonProperty("email")
    private String email;
    @JsonProperty("studentkey")
    private String studentKey;
    // GETTERS and SETTERS

    @Override
    public String toString() {
        return "Student{" + "profileRoot=" + profileRoot + ", nickname=" 
            + nickname + ", email=" + email + ", studentKey=" + studentKey + '}';
    }
}

因此,最终的映射看起来像

public class Students {
    @JsonProperty("students_key")
    Map<String, Student> students;
}

当我们测试它时,它按预期工作

public class StudentsTest {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();

        File file = new File("test.json");
        Students students = mapper.readValue(file, Students.class);

        for (Student s : students.getStudents().values()) {
            System.out.println(s);
        }
    }
}

结果

Student{profileRoot=/profile/student_key_one/, nickname=sam1, email=sam1@gmail.com, studentKey=student_key_one}
Student{profileRoot=/profile/student_key_two/, nickname=sam2, email=sam2@gmail.com, studentKey=student_key_two}
Student{profileRoot=/profile/student_key_three/, nickname=sam3, email=sam3@gmail.com, studentKey=student_key_three}
Student{profileRoot=/profile/student_key_four/, nickname=sam4, email=sam4@gmail.com, studentKey=student_key_four}
Student{profileRoot=/profile/student_key_five/, nickname=sam5, email=sam5@gmail.com, studentKey=student_key_five}