如何在ASP.NET MVC中返回JsonResult

Pur*_*ome 3 asp.net-mvc jquery json

我在Action方法中将以下数据作为字符串:

string json = "[[1,2],[3,4],[5,6]]";
Run Code Online (Sandbox Code Playgroud)

简单.

当我调用Json视图时,它将结果封装在两个双引号中.这会阻止客户端javascript将此结果加载到javascript对象中.

eg. 
return Json(json);

result => "[[1,2],[3,4],[5,6]]"
Run Code Online (Sandbox Code Playgroud)

但是,如果我将结果作为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?).
Run Code Online (Sandbox Code Playgroud)

那么,有人可以解释一下我应该做的正确吗?我觉得ContentResult不是正确的方法.

Mat*_*caj 6

我猜想JsonResult想要序列化你传入的对象.并且因为你的字符串或多或少'序列化'(用Json术语)所能做的只是看到对象是一个字符串,并在'Json land ',字符串文字得到它们周围的引号.

也许如果您将字符串更改为某种类型的强类型List/Collection/Array(表示字符串中的数据),它将正确序列化.