我有一个 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());
但是,我想要的输出是这样的:
应该做哪些事情才能达到我想要的结果?在我的 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/