您好,我正在尝试将字符串发送到看起来像 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 中它的渲染输出是这样的:
[{"title":"sdf sdfsd sdf sd f","description":"sdf sdf sd fsd sd sdf sdf dssd sdf sd s","latitude":53.740259851464685,"longitude":-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/