gpt4 book ai didi

c# - 将 IEnumerable 转换为 JSON 对象

转载 作者:行者123 更新时间:2023-11-30 21:07:00 25 4
gpt4 key购买 nike

使用 JSON.NET,我可以将通用列表序列化为 JSON 字符串:

 return Json(new { success = true, data = JsonConvert.SerializeObject(units) });

但是如何将通用列表序列化为 JSON 对象。这样做的好处是我在客户端不需要这个:

var jsonData = $.parseJSON(units);

最佳答案

不要序列化对象的一部分,序列化整个对象:

return Content(JsonConvert.SerializeObject(new { success = true, data = units }), "text/javascript");

内置的 Json 方法被硬编码为返回使用内置(有些受限).NET JavaScript 序列化程序的结果。如果您想获得像那样易于使用的东西,请将您自己的“Json”方法添加到与 Json.NET 做同样事情的基 Controller 类中。

关于c# - 将 IEnumerable<T> 转换为 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10783233/

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