Spring Boot-从数据库获取数据并将其存储在列表中,然后使用杰克逊将其解析为JSON

问题描述 投票:0回答:1

我正在尝试从多个表中获取数据并将其放入类的Array List中,然后将其转换为JSON Object。但是当我尝试使用Jackson Object Mapper将其解析为json时,所有列表的转换如下使用ObjectMapper().writeValueAsString从类对象反序列化到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"
                }
            ]

}
]
}

以上仅为示例。

请帮助获得所需的输出。

下面是使用的类文件。

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)
spring-boot jackson jsonserializer
1个回答
0
投票

如下所示的类结构将帮助您解析:

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;

}
© www.soinside.com 2019 - 2024. All rights reserved.