英文:
How to convert List<JsonObject> to JsonArray in JAVA
问题
我已经编写了一段代码,需要从一个“select * 查询”中创建一个JSON格式的输出。到目前为止,我能够获取以下输出,但我需要将其包装在“users”内:
{
"displayName": "Tony Stark",
"givenName": "Tony",
"surname": "Stark"
},
{
"displayName": "James Martin",
"givenName": "James",
"surname": "Martin"
}
我需要像这样的格式:
{
"users":
[
{
"displayName": "Tony Stark",
"givenName": "Toney",
"surname": "Stark"
},
{
"displayName": "James Martin",
"givenName": "James",
"surname": "Martin"
}
]
}
请帮助我解决这个问题。
英文:
I have written a code, where I need to create a JSON format output from a "select * query". So far I am able to get the output like this, but I have to wrap it up inside "users"
{
"displayName": "Tony Stark",
"givenName": "Tony",
"surname": "Stark"
},
{
"displayName": "James Martin",
"givenName": "James",
"surname": "Martin"
}
I need something like this:
{
"users":
[
{
"displayName": "Tony Stark",
"givenName": "Toney",
"surname": "Stark"
},
{
"displayName": "James Martin",
"givenName": "James",
"surname": "Martin"
}
]
}
Please help me out in this.
答案1
得分: 1
是的,如果您创建一个名为"Users"的模型类,并正确映射它以及其属性,那么它将与您在截图中显示的内容完全一样。
附上参考模型类。
英文:
Yes it's possible if you create a model class named Users, map it properly along it with it's attributes so it will show exactly as shown by you in the screenshot.
Attaching reference model class.
答案2
得分: 0
以下是已经翻译好的部分:
"如果添加一个包装类并将其转换为JSON将会很容易。"
import java.util.List;
public class UserWrap {
private List<User> users;
public UserWrap(List<User> users) {
this.users = users;
}
}
英文:
It would be easy if add a wrap class and convert it to json
import java.util.List;
public class UserWrap {
private List<User> users;
public UserWrap(List<User> users) {
this.users = users;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论