如何将普通的JSON对象转换为嵌套的JSON对象

时间:2019-11-10 05:37:38

标签: json java-8

我有14个普通的json对象,并希望形成一个嵌套的json输出

输入:

{
 "School": "Happy",
 "Teacher": "Mellisa",
 "grade": "sixth",
 "Student name": "Rob",
 "Subject": "botany"
}
{
 "School": "Happy",
 "Teacher": "Mellisa",
 "grade": "sixth",
 "Student name": "Rob",
 "Subject": "zoology"
}
{
 "School": "Happy",
 "Teacher": "Mellisa",
 "grade": "sixth",
 "Student name": "kevin",
 "Subject": "botany"
}
{
 "School": "Happy",
 "Teacher": "Mellisa",
 "grade": "sixth",
 "Student name": "kevin",
 "Subject": "botany"
}
{
 "School": "Happy",
 "Teacher": "Petter",
 "grade": "sixth",
 "Student name": "tim",
 "Subject": "botany"
}
{
 "School": "Happy",
 "Teacher": "Petter",
 "grade": "sixth",
 "Student name": "tim",
 "Subject": "zoology"
}
{
 "School": "Happy",
 "Teacher": "Petter",
 "grade": "sixth",
 "Student name": "david",
 "Subject": "botany"
}
{
  "School": "Happy",
  "Teacher": "Petter",
  "grade": "sixth",
  "Student name": "david",
  "Subject": "zoology"
}
{
  "School": "Happy",
  "Teacher": "Mellisa",
  "grade": "seventh",
  "Student name": "jane",
  "Subject": "math"
 }
{
  "School": "Happy",
  "Teacher": "Mellisa",
  "grade": "seventh",
  "Student name": "Lin",
  "Subject": "physics"
}
{
 "School": "Happy",
 "Teacher": "Mellisa",
 "grade": "seventh",
 "Student name": "jane",
 "Subject": "math"
}
{
  "School": "Happy",
  "Teacher": "Mellisa",
  "grade": "seventh",
  "Student name": "Lin",
  "Subject": "physics"
 }
{
  "School": "Happy",
  "Teacher": "Petter",
  "grade": "seventh",
  "Student name": "jane",
  "Subject": "math"
 }
{
  "School": "Happy",
  "Teacher": "Petter",
  "grade": "seventh",
  "Student name": "Lin",
  "Subject": "physics"
 }

输出:

{
"School": "Happy",
"Teacher": [
    {
        "name": "petter",
        "Student": [
            {
                "name": "Lin",
                "grade":"seventh",
                "Subject": [
                    {
                        "name": "physics"
                    },
                    {
                        "name": "math"
                    }
                ]
            },
            {
                "name": "Jane",
                "grade":"seventh",
                "Subject": [
                    {
                        "name": "physics"
                    },
                    {
                        "name": "math"
                    }
                ]
            }
        ]
    }
]
}

1 个答案:

答案 0 :(得分:0)

嗯,这个问题太冗长了!并且需要了解一些josn库。(我使用this library

首先,您需要定义一个类以将json数据映射到它。由于某些json键以大写字母开头,因此与Java语法不同,我使用了@JsonProperty注释。

 class JsonData {
    @JsonProperty("School")
    private String school;
    @JsonProperty("Teacher")
    private String teacher;
    private String grade;
    @JsonProperty("Student name")
    private String studentName;
    @JsonProperty("Subject")
    private String subject;
}

要将json字符串转换为对象,可以使用以下代码:

ObjectMapper mapper = new ObjectMapper();
List<JsonData> list = mapper.readValue(jsonString, new TypeReference<List<JsonData>>() {}); 

将json字符串转换为对象后,将其分组为几个属性。(学校,老师,学生)

Map<String, Map<String, Map<String, List<JsonData>>>> result = list.stream()
         .collect(Collectors
                 .groupingBy(JsonData::getSchool, Collectors
                        .groupingBy(JsonData::getTeacher, Collectors
                            .groupingBy(JsonData::getStudentName, Collectors.toList()))));  

它的输出是:

{Happy=
   {Mellisa=
    {Lin=[
      JsonData{school='Happy', ....},
      JsonData{school='Happy', ....}],

在最后一步中,通过遍历先前的结果,将创建最终对象。

ResultData resultData = null;
Teacher teacher = null;
Student student;
for (Map.Entry<String, Map<String, Map<String, List<JsonData>>>> entry : result.entrySet()) {
    resultData = new ResultData(entry.getKey());
   for (Map.Entry<String, Map<String, List<JsonData>>> entry1 : entry.getValue().entrySet()) {
        teacher = new Teacher(entry1.getKey());
        for (Map.Entry<String, List<JsonData>> entry2 : entry1.getValue().entrySet()) {
             student = new Student(entry2.getKey());
             for (JsonData jsonData : entry2.getValue()) {
                    student.setGrade(jsonData.getGrade());
                    student.getSubject().add(new Subject(jsonData.getSubject()));
                }
                teacher.getStudent().add(student);
            }
        }
        resultData.getTeacher().add(teacher);
    }

在此使用的其他类:

class ResultData {
  private String school;
  private List<Teacher> teacher = new ArrayList<>();
}

class Teacher {
  private String name;
  private List<Student> student = new ArrayList<>();
}

class Student {
  private String name;
  private String grade;
  private List<Subject> subject = new ArrayList<>();
}

class Subject {
  private String name;
}