JavaScript反序列化无法反序列化JSON对象
JavaScript Deserialize could not Deserialize JSON Object
我正在 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 对象字符串在备注 属性 处意外终止,因为它包含 & 符号。
我应该如何处理这个特殊字符和其他特殊字符,如<、>等?
您提供的 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。以下代码对我来说工作正常。
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; }
}
}
我正在 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 对象字符串在备注 属性 处意外终止,因为它包含 & 符号。
我应该如何处理这个特殊字符和其他特殊字符,如<、>等?
您提供的 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。以下代码对我来说工作正常。
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; }
}
}