How to return JsonResult to ASP.NET MVC

I have the following data as a string in the Action method:

string json = "[[1,2],[3,4],[5,6]]";

Simple

When I call the Json view, it encapsulates the result in two double quotes. This stops the client side javascript from loading this result into the javascript object.

eg. 
return Json(json);

result => "[[1,2],[3,4],[5,6]]"

but if I return the result as ContentResult, the result will be loaded into the javascript object, and I can do everything I need with it.

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?).

So, can someone explain what I should do right, please? I feel ContentResult is the wrong way to do this.

+3
source share
2 answers

, JsonResult , . "" ( Json), , , , , "Json land" .

, List/Collection/Array - ( , , ), .

+6

, , , , , Javascript:

var jsonResult = eval(resultWithQuotes);

, , , script.

+1

Source: https://habr.com/ru/post/1708137/


All Articles