c# – Json.Encode()是否使用JavaScriptSerializer类进行序列化
作者:互联网
Json.Encode()Helper是否使用JavaScriptSerializer类将字符串编码为json?
我在使用Json.Encode(Model)时遇到循环引用异常,即使我正在序列化的类属性具有[ScriptIgnore]属性.
我唯一的猜测是,Json.Encode()助手可能不会使用JavaScriptSerializer序列化为json,但我无法在msdn上找到任何文档.
@Html.Raw(Json.Encode(Model))
以下是其中一个具有不应序列化的属性的模型的示例…
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Script.Serialization;
namespace RobotDog.Entities {
public class Character {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[MaxLength(200)]
public string Name { get; set; }
public virtual Person Person { get; set; }
[ScriptIgnore]
public virtual Movie Movie { get; set; }
}
}
解决方法:
Does the Json.Encode() Helper use the JavaScriptSerializer class to encode a string to json?
是.
从源代码:
private static readonly JavaScriptSerializer _serializer = Json.CreateSerializer();
public static string Encode(object value)
{
DynamicJsonArray dynamicJsonArray = value as DynamicJsonArray;
if (dynamicJsonArray != null)
return Json._serializer.Serialize((object) (object[]) dynamicJsonArray);
else
return Json._serializer.Serialize(value);
}
其中JavaScriptSerializer是System.Web.Script.Serialization.JavaScriptSerializer
也是为了帮助你解决问题,请参阅this thread
标签:json,c,razor,javascriptserializer 来源: https://codeday.me/bug/20190517/1123303.html