JavaScript反序列化无法反序列化JSON对象

时间:2016-08-23 12:41:08

标签: c# asp.net ajax c#-4.0 asp.net-ajax

我在ASP.NET应用程序中通过AJAX发布JSON对象。

{
    "SaveData" : "{
       "TransactionType":"2",
       "Date":"8/10/2016",
       "BankAccountID":"449",
       "PaidTo":"Cash",
       "Amount" :"1551",
       "CheckNumber":"51451",
       "SupportingDocNo":"51521",
       "Remarks":"This is a remarks & this contains special character",
       "CheckPaymentID":0
    }", 
    "Type" : "Save"
}

在服务器端(我正在使用处理程序)我已将ContentType设置为application / json 并将SaveData对象反序列化为

context.Request.ContentType = "application/json";
var data = new JavaScriptSerializer()
           .Deserialize<CheckPaymentsService>(context.Request["SaveData"]);

通过执行此操作,我的SaveData对象字符串在Remarks属性中意外终止,因为它包含&amp;登录。

我该如何处理这个特殊字符和其他特殊字符,例如&lt;,&gt;等等?

2 个答案:

答案 0 :(得分:0)

您提供的json无效。

这是正确的版本(您可以在http://jsonlint.com/上查看):

{
    "SaveData": {
        "TransactionType": "2",
        "Date": "8/10/2016",
        "BankAccountID": "449",
        "PaidTo": "Cash",
        "Amount": "1551",
        "CheckNumber": "51451",
        "SupportingDocNo": "51521",
        "Remarks": "This is a remarks & this contains special character",
        "CheckPaymentID": 0
    },
    "Type": "Save"
}

此外,json中的非法字符

‘ single quote
” quote
\ backslash

答案 1 :(得分:0)

我认为你需要逃脱你的json。以下代码对我来说很好。

{{1}}
相关问题