c#使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 placeList = new List(); //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;
在视图中它的渲染输出如下:
[{"title":"sdf sdfsd sdf sd f","description":"sdf sdf sd fsd sd sdf sdf dssd sdf sd s","latitude":53.740259851464685,"longitude":-2.4602634343627927},
我如何让它在视图中呈现为普通的json? 减去"
等等?
在下面的评论中,您说您的视图正在使用@ViewBag.Places
你在用Razor吗? 如果是这样, @
语法与<%:
- 的作用相同 - 它对内容进行编码。
使用IHtmlString
接口来避免它,所以:
ViewBag.Places = new HtmlString(sJSON);
要么
@HtmlString(ViewBag.Places)
@Html.Raw(ViewBag.Places)
也有效
你试过了吗 ?
string sJSON = HttpServerUtility.HmltDecode(oSerializer.Serialize(placeList));