JSON.net序列化的C#对象JSON问题 [英] JSON.net Serialize C# object to JSON Issue

查看:92
本文介绍了JSON.net序列化的C#对象JSON问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用序列库JSON.net一个C#对象JSON。我遇到的问题是正在创建的字符串具有与放大器; QUOT在其中的

I am trying to serialize a C# object to JSON using JSON.net library. The issue I am having is the string being created has &quot's in it.

下面通过JsonConvert.SerializeObject返回的字符串:

Below is the string returned via 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对象:

Below is the object I am trying to serialize into 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; }
}

我是不是做错了什么?我已搜查,似乎其他人谁使用的方法获取清洁串!感谢您的时间提前!

Am I doing something wrong? I have searched and it seems others who use the method get cleaner strings! Thank you for your time in advance!

推荐答案

您JSON字符串正在HTML EN codeD。既然你呈现JSON在您看来,您可以从被连接codeD使用 @ Html.Raw()帮手prevent它。

Your json string is being HTML encoded. Since you're rendering the json in your view, you can use the @Html.Raw() helper to prevent it from being encoded.

var data = { json : "@Html.Raw(JsonConvert.SerializeObject(Model))" };

这篇关于JSON.net序列化的C#对象JSON问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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