c#使json无法在视图中正确呈现

rak*_*los 10 c# asp.net-mvc json

嗨我试图发送一个字符串到一个看起来像json的视图.

我发送一份名单:

class Place 
        {
            public string title { get; set; }
            public string description { get; set; }
            public double latitude { get; set; }
            public double longitude { get; set; }
        }

List<Place> placeList = new List<Place>(); 
//add places to PlaceList

//Then i do this
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string sJSON = oSerializer.Serialize(placeList);
            ViewBag.Places = sJSON;
Run Code Online (Sandbox Code Playgroud)

在视图中它的渲染输出如下:

[{&quot;title&quot;:&quot;sdf sdfsd sdf sd f&quot;,&quot;description&quot;:&quot;sdf sdf sd fsd sd sdf sdf dssd sdf sd s&quot;,&quot;latitude&quot;:53.740259851464685,&quot;longitude&quot;:-2.4602634343627927},
Run Code Online (Sandbox Code Playgroud)

我如何让它在视图中呈现为普通的json?减去&quot;等?

Kei*_*ith 20

在下面的评论中,您说您的观点正在使用 @ViewBag.Places

你在用Razor吗?如果是这样,@语法与它相同<%:- 它对内容进行编码.

使用IHtmlString界面来避免它,所以:

ViewBag.Places = new HtmlString(sJSON);
Run Code Online (Sandbox Code Playgroud)

要么

@HtmlString(ViewBag.Places)
Run Code Online (Sandbox Code Playgroud)


stu*_*net 5

@Html.Raw(ViewBag.Places)
Run Code Online (Sandbox Code Playgroud)

也有效