我试图使用JSON.net库将一个C#对象序列化到JSON。我遇到的问题是正在创建的字符串有& quot;。
以下是通过JsonConvert.SerializeObject返回的字符串:
{ "ComId": "AAAiB+AAHAALOaFAAL","CovId": "AAABC9AAPAAAZYUAAI","EffectiveDate": "\\/Date(1329368400000-0500)\\/","ExpirationDate": "\\/Date(1360990800000-0500)\\/","State": "TX","DeductibleAmount": 500.0,"DeductibleType": "PD" }
运行字符串通过JSONLint返回:
Parse error on line 1: { "ComId": & -----^ Expecting 'STRING','}'
下面是我试图序列化成JSON的对象:
public class CommonInfoModel { public virtual string ComId { get; set; } public virtual string CovId { get; set; } [Display(Name = "Effective Date")] public virtual DateTime EffectiveDate { get; set; } [Display(Name = "Expiration Date")] public virtual DateTime ExpirationDate { get; set; } [Display(Name = "State")] public virtual string State { get; set; } [Display(Name = "Deductible Amount")] public virtual decimal DeductibleAmount { get; set; } [Display(Name = "Deductible Type")] public virtual string DeductibleType { get; set; } }
你的json字符串是HTML编码的。由于您在视图中呈现json,您可以使用@ Html.Raw()帮助器来防止编码。
原文链接:https://www.f2er.com/json/288583.htmlvar data = { json : "@Html.Raw(JsonConvert.SerializeObject(Model))" };