c# 到 json 无法在 View 中正确呈现

标签 c# asp.net-mvc json

您好,我正在尝试将字符串发送到看起来像 json 的 View 。

我正在发送地点列表:

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;

虽然在 View 中它的渲染输出是这样的:

[{&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},

如何让它在 View 中呈现为普通 json?减去 等?

最佳答案

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

您在使用 Razor 吗?如果是 @语法与 <%: 的作用相同- 它对内容进行编码。

使用 IHtmlString接口(interface)来避免它,所以要么:

ViewBag.Places = new HtmlString(sJSON);

或者

@HtmlString(ViewBag.Places)

关于c# 到 json 无法在 View 中正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4605449/

相关文章:

c# - 在 C# 中转换为泛型类型

c# - 在Installshield中添加msi的先决条件

asp.net-mvc - 如何让用户将格式化文本、图像和附件插入 MVC 编辑字段?

c# - 如何获取列表中资源文件的名称?

c# - 401 未经授权的异常与 SSL 的 WebRequest

c# - ToDictionary 从第二个表查询返回 Null

c# - 使用 aspnet 成员(member)提供程序进行用户管理

mysql - 获取字符串数据作为 Json 数据

java - 改造:使用 Jackson 反序列化失败,没有任何错误

Javascript:根据键的值有条件地过滤 JSON?