转换对象到JSON在MVC 4 [英] Convert Object to JSON in MVC 4

查看:141
本文介绍了转换对象到JSON在MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是对象转换为JSON 的JavaScriptSerializer ,我可以看到在服务器code此JSON输出:

I am converting an object to JSON using JavaScriptSerializer and I can see this JSON output in server code:

[{"UserId":1,"UserName":"Admin"}]

但在UI它变得转换为类似下面

But in the UI it's getting converted to something like below

[{"UserId":1,"UserName":"Admin"}].

如何逃生者&放大器; QUOT;

推荐答案

如果您使用的是你需要使用 Html.Raw 法的Razor视图引擎:

If you are using the Razor view engine you need to use the Html.Raw method:

<script type="text/javascript">
    var model = @Html.Raw(Json.Encode(Model));
</script>

请注意这是短,相当于新的JavaScriptSerializer()。序列化()在 Json.En code 方法的使用

Notice the usage of the Json.Encode method which is shorter and equivalent to new JavaScriptSerializer().Serialize().

这篇关于转换对象到JSON在MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆