我正在尝试使用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; }
}
我做错了吗?我搜索过,似乎其他使用该方法的人获得了更清晰的字符串!感谢您提前的时间!
答案 0 :(得分:91)
您的json字符串是HTML编码的。由于您在视图中呈现json,因此可以使用@Html.Raw()
帮助程序来防止对其进行编码。
var data = { json : "@Html.Raw(JsonConvert.SerializeObject(Model))" };