Spring 引导 - 从数据库获取数据并将其存储在列表中并使用 jackson 将其解析为 JSON

Spring Boot - Get Data from DB and store it in list and parse it to JSON using jackson

我试图从多个表中获取数据并将其放入 class 的数组列表中,然后将其转换为 JSON 对象。 但是当我尝试使用 Jackson Object Mapper 将其解析为 json 时,所有列表都按如下方式转换 使用 ObjectMapper().writeValueAsString 将 class 对象反序列化为 json

```{
"College": [
{
"institution": [
                {
                    "instId": "T34",
                    "Country": "India",
                    "Code": "T33"
                },
                {
                    "instId": "T22",
                    "Country": "India",
                    "Code": "T22"
                }
                ],
"Rating": [
                {
                    "star": "4"
                    "comments": "good"
                },
                {
                    "star": "2"
                    "comments": "ok"
                },
}
]
}```

但我想要如下结果

{
"College": [
{
"institution": [
                {
                    "instId": "T34",
                    "Country": "India",
                    "Code": "T33"
                }
                ],
"Rating": [
                {
                    "star": "4"
                    "comments": "good"
                }
            ]

},
{
"institution": [
                {
                    "instId": "T22",
                    "Country": "India",
                    "Code": "T22"
                }
                ],
"Rating": [
                {
                    "star": "2"
                    "comments": "ok"
                }
            ]

}
]
}

以上仅为示例

请帮助获得所需的输出。

以下是使用的 class 个文件。

public class AllCollege{
List<College> college = new ArrayList<>();

public List<College> getCollege() {
        return college;
    }

    public void setCollege(List<College> college) {
        this.college = college;
    }

}


public class College{

private List<Institution> institution = new ArrayList<>();
private List<Rating> rating = new ArrayList<>();

    public List<Institution> getInstitution() {
        return institution;
    }

    public void setInstitution(List<Institution> institution) {
        this.institution = institution;
    }

    public List<Rating> getRating() {
        return rating;
    }

    public void setRating(List<Rating> rating) {
        this.rating = rating;
    }

}



public class Institution {

    private String instId;
    private String country;
    private String code;

    public String getInstId() {
        return instId;
    }
    public void setInstId(String instId) {
        this.instId = instId;
    }

    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
}


public class Rating {

    private String star;
    private String comments;

    public String getStar() {
        return star;
    }
    public void setStar(String star) {
        this.star = star;
    }

    public String getComments() {
        return comments;
    }
    public void setComments(String comments) {
        this.comments = comments;
    }

}

下面是将表中的数据设置到 ArrayList 中,然后转换为 json 字符串。

session = sessionFactory.openSession();
String sql = "from institution";
Query<InstDto> query = session.createQuery(sql);
List<Institution> configdtoList =query.list();
College alc = new College();
alc.setInstitution(configdtoList);
.
.
.
similarly Rating table.
List<College> clist = new new ArrayList<>();
clist.add(alc);
AllCollege ac = new AllCollege();
ac.setCollege(clist);

String responseJson = new ObjectMapper().writeValueAsString(ac)

class结构如下,可以帮你解析:

public class Sample {

@JsonProperty("College")
private List<College> college;

}

public class College {

private List<Institution> institution;
@JsonProperty("Rating")
private List<Rating> rating;

}

public class Rating {

private String comments;
private String star;

}
 public class Institution {

@JsonProperty("Code")
private String code;
@JsonProperty("Country")
private String country;
private String instId;

}

我创建了一个包含 List<AllCollege> 作为值的 HashMap,然后使用了按预期工作的 json 解析器。