【发布时间】:2020-07-25 17:26:33
【问题描述】:
我有这些 POJO
class LocaleBook
{
private String bookId;
private String bookName;
private Locale locale;
//setters & getters
}
class Locale
{
private String country;
private String language;
//setters & getters
}
上面的示例数据是
"alice", "Alice in the wonderland", "GB", "en"
"matrix", "The Matrix", "TH", "en"
"christtree", "The Christmas Tree", "GB", "en"
还有这个方法:
List<LocaleBook> localeBooks = repo.getLocalBooks();
我想按国家和语言将数据分组在一起,我创建了这个 POJO:
class NewPoJo
{
private String country;
private String language;
private List<KeyValuePair> localeBooks = new HashSet<>();
//setters & getters
}
class KeyValuePair {
private String label;
private String value;
//setters & getters
}
预期输出:
[
{
"country":"GB",
"language":"en",
"localeBooks":[
{
"label":"alice",
"value":"Alice in the wonderland"
},
{
"label":"christtree",
"value":"The Christmas Tree"
}
]
},
{
"country":"TH",
"language":"en",
"localeBooks":[
{
"label":"matrix",
"value":"The Matrix"
}
]
}
]
我通过country 和language 尝试了Collectors.groupingBy,但似乎它会产生类似关注的东西,这不是我想要的。我怎样才能达到我想要的预期输出?
{
"GB_en": [
{
"label": "alice"
"value": "Alice in the wonderland"
},
{
"label": "christtree"
"value": "The Christmas Tree"
}
]
}
【问题讨论】:
标签: json spring-boot java-8 grouping