gpt4 book ai didi

java - 如何在JSON中添加根节点

转载 作者:行者123 更新时间:2023-11-30 08:01:29 25 4
gpt4 key购买 nike

我需要在 JSON 响应中包含顶部节点。我在 REST Web 服务中的 JSON 响应:

[
{
"address": "delhi",
"fristname": "xxxx",
"id": 1,
"lastname": "xxxx",
"phone": "0000000"
},
{
"address": "ssss",
"fristname": "yyyy",
"id": 2,
"lastname": "yyyyy",
"phone": "0000000"
},
{
"address": "wwww",
"fristname": "aaaa",
"id": 3,
"lastname": "aaaaa",
"phone": "0000000"
}
]

我想要这样的 JSON 响应:

"employee": [
{
"address": "delhi",
"fristname": "xxxx",
"id": 1,
"lastname": "xxxx",
"phone": "0000000"
},
{
"address": "ssss",
"fristname": "yyyy",
"id": 2,
"lastname": "yyyyy",
"phone": "0000000"
},
{
"address": "wwww",
"fristname": "aaaa",
"id": 3,
"lastname": "aaaaa",
"phone": "0000000"
}
]

请告诉我如何添加根节点 JSON。提前致谢。

最佳答案

你可以在Java中这样做。创建一个新的 JSON 对象并将数组放入其中。

JSONObject myobj = new JSONObject(); 
myobj.put("employees", <your_json_array>);

关于java - 如何在JSON中添加根节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37611491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com