【问题标题】:JavaScript Deserialize could not Deserialize JSON ObjectJavaScript 反序列化无法反序列化 JSON 对象
【发布时间】:2016-12-30 07:15:55
【问题描述】:

我在我的 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 属性中意外终止,因为它包含 & 符号。

我应该如何处理这个特殊字符和其他特殊字符,如 等?

【问题讨论】:

  • 您发布的“JSON”从一开始就不是有效的 JSON,这可能没有帮助。
  • 即使在更正 JSON 后也出现同样的错误
  • 请显示更正后的 JSON...,最好提供 minimal reproducible example。当您明确反序列化时,您应该能够使用字符串和反序列化调用编写控制台应用程序。
  • 现在发布的 JSON 类似于以下内容。 { "SaveData": { "TransactionType": "2", "Date": "8/10/2016", "BankAccountID": "447", "PaidTo": "Cash", "Amount": "1551", “CheckNumber”:“15155”,“SupportingDocNo”:“56”,“Remarks”:“这是一个备注&这个包含特殊字符”,“CheckPaymentID”:0},“Type”:“Save”}方式我已经反序列化它上面给出的完全正确。如果我用 & 替换它并且它可以工作。我用 [Serializable] 属性装饰了 CheckPaymentsService 类
  • 不,请编辑帖子,而不是在 cmets 中添加 JSON...,然后提供 minimal reproducible example。我们应该可以复制、粘贴、编译、运行,看看问题。

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


【解决方案1】:

我认为您需要转义您的 json。以下代码对我来说很好。

using System;
using System.Web.Script.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        private const string 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""}";

        static void Main(string[] args)
        {
            try
            {
                var data = new JavaScriptSerializer().Deserialize<CheckPaymentsService>(Json);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }

    public class CheckPaymentsService
    {
        public SaveData SaveData { get; set; }
        public string Type { get; set; }
    }

    public class SaveData
    {        
        public int TransactionType { get; set; }
        public DateTime Date { get; set; }
        public int BankAccountID { get; set; }
        public string PaidTo { get; set; }
        public int Amount { get; set; }
        public int CheckNumber { get; set; }
        public int SupportingDocNo { get; set; }
        public string Remarks { get; set; }
        public int CheckPaymentID { get; set; }
    }
}

【讨论】:

    【解决方案2】:

    您提供的 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
    

    【讨论】:

    • 更正了 JSON 格式仍然有同样的错误@Quentin Roger
    猜你喜欢
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多