我的 Action 方法中有以下字符串数据:
string json = "[[1,2],[3,4],[5,6]]";
简单。
当我调用 Json View 时,它将结果封装在两个双引号中。这会阻止客户端 JavaScript 将此结果加载到 JavaScript 对象中。
eg.
return Json(json);
result => "[[1,2],[3,4],[5,6]]"
但是,如果我将结果作为 ContentResult 返回,那么结果将被加载到 JavaScript 对象中,我可以用它做任何我需要做的事情。
eg.
return new ContentResult
{
Content = json,
ContentType = "application/json",
ContentEncoding =System.Text.Encoding.UTF8
};
result => [[1,2],[3,4],[5,6]]
(notice how the double quotes are missing?).
那么,有人可以解释一下我应该做什么吗?我觉得 ContentResult 不是正确的方法。
最佳答案
我猜想 JsonResult
想要序列化您传入的对象。因为您的字符串或多或少是“序列化”的(用 Json 术语来说),所以它所能做的就是看到该对象是一个字符串,在“Json land”中,字符串文字周围有引号。
也许如果您将字符串更改为某种强类型列表/集合/数组(表示字符串中的数据),它会正确序列化。
关于jquery - 如何在 ASP.NET MVC 中返回 JsonResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/851160/