【发布时间】:2019-11-24 22:56:22
【问题描述】:
我正在使用 MVC 模式在 .Net Core 2.2 中工作。我有一个 Web-API 控制器,在那里我使用经典的 CRUD 方法创建了一些端点。
现在我已经编译了所有内容,当我调试我的解决方案时,我得到一个包含几个 JSON 格式对象的数组 - 但系统将输出视为一个数组,而不是 JSON。
现在我希望这个数组变成 JSON。
我看过这个:https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm
我的原始方法看起来像这样(在控制器中):
[HttpGet("/ListAllItems")]
public async Task<IEnumerable<DtoModel>> ListAllItemsFromDb()
{
return await _dbProvider.ListAllItemsAsync();
}
并给出以下输出:
[{"id": "GUID-STRING", "itemName": "foo"}, {"id": "GUID-STRING2", "itemName": "bar"}]
//My frontend did not recognize this as a JSON, since it is an array, and threw an exception
所以我在我的控制器中尝试了这个
[HttpGet("/ListAllItems")]
public async Task<JObject> ListAllItemsFromDb()
{
var result = await _dbProvider.ListAllItemsAsync();
string output = result.ToString();
string json = JsonConvert.SerializeObject(output);
JObject obj = JObject.Parse(json);
return obj;
}
当我运行此代码时,我的错误消息指出:“JsonReaderException:从 JsonReader 读取 JObject 时出错。当前 JsonReader 项不是对象:字符串。路径 '',第 1 行,位置 60。”
如何将对象数组转换为 JSON 对象?
【问题讨论】:
-
但是看起来您的数据不是 JSON 对象,而是 JSON 对象数组
-
//我的前端没有将其识别为 JSON 那么问题出在您的浏览器/前端。这是 json 对象数组的正确语法。你如何在前端解析它?
-
这有这么多糟糕的决定,我什至无法开始,你为什么使用JObject作为你的行动的结果,你为什么序列化然后序列化?为什么不声明这样的对象 var result = new { items = await _dbProvider.ListAllItemsAsync()};
-
but the system sees the output it as an array, not as JSON.如果您像您一样传递两个对象,那么您将获得 JSON(正如您在问题中显示的那样)。它是 100% 合法的 JSON。该 JSON 在反序列化时将产生一个对象数组(这是有道理的——你有两个对象)。 如果您不希望这样,我们需要查看您的 JS 代码以了解您是如何处理这个 JSON 的。 -
@Emilia 这几乎可以肯定是一个 XY 问题 (meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。某些东西不起作用,您已经对问题的性质得出了结论。我们确实需要看一下JS代码,看看你对问题的猜测是否准确。