gpt4 book ai didi

c# - 在 ASP.net API 核心 2.1 中向 JSON 输出添加部分

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:27 25 4
gpt4 key购买 nike

我正在使用 ASP.NET Core API 通过 GET 方法输出一个 JSON 数组。JSON结构如下:

原文:

[{
"Title": "My aswsome title_1",
"ID": "1",
"Settings": 32
},
{
"Title": "My aswsome title_2",
"ID": "2",
"Settings": 33
}]

我查看了网络,但找不到如何将 Section 名称添加到整个 JSON 数组。

最佳答案

这里假设原始操作只返回一个数组

[HttpGet]
public IActionResult MyAction() {
Myitems[] array;

//...array populate

return Ok(array);
}

产生当前结果。

要包装数组创建一个匿名类型

[HttpGet]
public IActionResult MyAction() {
Myitems[] array;

//...array populate

return Ok(new { MyArraySection = array });
}

会产生

{
"MyArraySection" : [
{
"Title": "My aswsome title_1",
"ID": "1",
"Settings": 32
},
{
"Title": "My aswsome title_2",
"ID": "2",
"Settings": 33
}]
}

关于c# - 在 ASP.net API 核心 2.1 中向 JSON 输出添加部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50897942/

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