我正在尝试使用JSON.net库将C#对象序列化为JSON。我遇到的问题是正在创建的字符串有"在里面。
以下是通过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()
帮助程序来防止对其进行编码。
var data = { json : "@Html.Raw(JsonConvert.SerializeObject(Model))" };