javascript - 如何删除 JSON 返回 C# 中的列名称

标签 javascript c# arrays json api

我有一个 JSON 输出如下:

[
    {
        positionCode: "POS1",
        positionName: "POSITION 1",
        positionDescription: "",
        parentPosition: "POS2",
    },
    {
        positionCode: "POS2",
        positionName: "POSITION 2",
        positionDescription: "",
        parentPosition: "POS3",
    }
]

这个 JSON 结果来 self 的 Web API,如下所示:

    return new JsonResult(query.Select(x => 
    new { x.PositionCode, x.PositionName , x.PositionDescription , x.ParentPosition }).ToList());

但是,我想要的输出是这样的:

enter image description here

应该做哪些事情才能达到我想要的结果?在我的 javascript 代码中还是在我的 C# Web API 中?谢谢!

最佳答案

假设您有与此类似的设置:

var data = new List<PositionClass>
{
    new PositionClass { PositionCode = "POS1", PositionName = "POSITION 1", PositionDescription = "", ParentPosition = "POS2" },
    new PositionClass {  PositionCode = "POS2", PositionName = "POSITION 2", PositionDescription = "", ParentPosition = "POS2" }
};

您有两个选择:

在 C# 代码中处理它

看起来您已经有 linq ,所以只需将您的 List<T>到数组或字符串中:

data.Select(d => new[] {d.ParentPosition, d.PositionCode, d.PositionDescription, d.PositionName}).ToArray()

在 JavaScript 代码中处理它

const response = [{
    positionCode: "POS1",
    positionName: "POSITION 1",
    positionDescription: "",
    parentPosition: "POS2",
  },
  {
    positionCode: "POS2",
    positionName: "POSITION 2",
    positionDescription: "",
    parentPosition: "POS3",
  }
];

const reformatted = response.map(r => Object.values(r))

console.log(reformatted);

关于javascript - 如何删除 JSON 返回 C# 中的列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58071730/

相关文章:

javascript - jQuery秒表,计算平均值并显示

javascript - Angular:如何使用 ViewChild 更改 CSS

c# - 使用 C# 通过 Windows 服务打印 PDF

c# - C#如何创建类的实例?

javascript - Tic Tac Toe 获胜功能

javascript - 谷歌街景图像精度

c# - 使用 BeginTransaction() 的 Entity Framework

arrays - 为什么我不能在 Swift 中使用数组的 append()?

javascript - "Remove"评估为 true 时的测试条件

PHP合并数组而不覆盖数据