反序列化JSON没有给出预期的结果

时间:2015-03-04 02:09:14

标签: c# json serialization

我使用var x1 = Request["jsonData"];

将json字符串发送到.cshtml页面

json文件在上一页的javascript中是urlEncoded,看起来像这样。

var x1 = Request["jsonData"];
//var x2 = "%5B%7B%22Author%22%3A%22John%20andeberg%22%2C%22ClosedDate%22%3Anull%2C%22Comment%22%3A%22%22%2C%22ComVal%22%3A2%2C%22CreatedDate%22%3A%222012-05-04T14%3A32%3A00%22%2C%22Description%22%3A%22testing%22%2C%22DType%22%3Anull%2C%22DueDate%22%3A%222012-07-17T00%3A00%3A00%22%2C%22ID%22%3A30%2C%22Name%22%3A%22Meet%20with%20technical%20teams%20to%20discuss%20solution%22%2C%22OtherID%22%3Anull%2C%22PercentComplete%22%3A0%2C%22Priority%22%3A141%2C%22PriorityName%22%3A%22Urgent%22%2C%22ProjectRef%22%3A43%2C%22ResID%22%3A1%2C%22ResName%22%3A%22John%20Vandeberg%22%2C%22Responsible%22%3A%22John%20Vandeberg%22%2C%22Status%22%3A138%2C%22StatusName%22%3A%22Open%22%2C%22UpdateBy%22%3A%22John%20Vandeberg%22%2C%22UpdateDate%22%3A%222013-06-10T11%3A08%3A00%22%7D%5D";
x2 = HttpUtility.UrlDecode(x1);

所以字符串被解码为HttpUtility.UrlDecode(x1),它给出了。

    "[{\"Author\":\"John  Vandeberg\",\"ClosedDate\":null,\"Comment\":\"\",\"ComVal\":3,\"CreatedDate\":\"2012-05-04T14:32:00\",\"Description\":\"\",\"DType\":null,\"DueDate\":\"2012-06-20T00:00:00\",\"ID\":29,\"Name\":\"Review the detailed project management plan\",\"OtherID\":null,\"PercentComplete\":0,\"Priority\":142,\"PriorityName\":\"High\",\"ProjectRef\":43,\"ResID\":null,\"ResName\":null,\"Responsible\":\"John  Vandeberg\",\"Status\":138,\"StatusName\":\"Open\",\"UpdateBy\":null,\"UpdateDate\":null},{\"Author\":\"John  Vandeberg\",\"ClosedDate\":null,\"Comment\":\"\",\"ComVal\":1,\"CreatedDate\":\"2012-05-04T14:33:00\",\"Description\":\"Add Description\",\"DType\":null,\"DueDate\":\"2013-06-28T00:00:00\",\"ID\":31,\"Name\":\"Discuss recruitment with HR for required BA's\",\"OtherID\":null,\"PercentComplete\":0,\"Priority\":142,\"PriorityName\":\"High\",\"ProjectRef\":43,\"ResID\":null,\"ResName\":null,\"Responsible\":\"John  Vandeberg\",\"Status\":138,\"StatusName\":\"Open\",\"UpdateBy\":\"John  Vandeberg\",\"UpdateDate\":\"2012-05-07T21:25:00\"},{\"Author\":\"John  Vandeberg\",\"ClosedDate\":null,\"Comment\":\"Test1\",\"ComVal\":1,\"CreatedDate\":\"2012-03-26T10:37:00\",\"Description\":\"PO Approved\",\"DType\":null,\"DueDate\":\"2013-06-28T00:00:00\",\"ID\":20,\"Name\":\"Get standard documentation from PMO 1\",\"OtherID\":null,\"PercentComplete\":2,\"Priority\":142,\"PriorityName\":\"High\",\"ProjectRef\":43,\"ResID\":null,\"ResName\":null,\"Responsible\":\"John  Vandeberg\",\"Status\":138,\"StatusName\":\"Open\",\"UpdateBy\":\"John  Vandeberg\",\"UpdateDate\":\"2012-04-07T23:29:00\"},{\"Author\":\"John  Vandeberg\",\"ClosedDate\":null,\"Comment\":\"\",\"ComVal\":1,\"CreatedDate\":\"2012-05-04T14:30:00\",\"Description\":\"\",\"DType\":null,\"DueDate\":\"2013-06-28T00:00:00\",\"ID\":27,\"Name\":\"Schedule meeting with senior stakeholders for scope clarification\",\"OtherID\":null,\"PercentComplete\":0,\"Priority\":142,\"PriorityName\":\"High\",\"ProjectRef\":43,\"ResID\":null,\"ResName\":null,\"Responsible\":\"John  Vandeberg\",\"Status\":138,\"StatusName\":\"Open\",\"UpdateBy\":null,\"UpdateDate\":null},{\"Author\":\"John  Vandeberg\",\"ClosedDate\":null,\"Comment\":\"\",\"ComVal\":2,\"CreatedDate\":\"2012-05-04T14:31:00\",\"Description\":\"\",\"DType\":null,\"DueDate\":\"2013-07-31T00:00:00\",\"ID\":28,\"Name\":\"Review previous PCB minutes and provide feedback\",\"OtherID\":null,\"PercentComplete\":0,\"Priority\":142,\"PriorityName\":\"High\",\"ProjectRef\":43,\"ResID\":null,\"ResName\":null,\"Responsible\":\"John  Vandeberg\",\"Status\":138,\"StatusName\":\"Open\",\"UpdateBy\":\"John  Vandeberg\",\"UpdateDate\":\"2013-09-27T21:10:00\"}]";

x2 = x2.Replace("\\", string.Empty);(我尝试使用此函数替换转义字符,但它什么都不做)x2.Replace(@"\", string.Empty);也没有做任何事情。

然后我将结果放入动态数组中

dynamic jsonData = Newtonsoft.Json.JsonConvert.DeserializeObject(x);

但这给出了一个奇怪的输出。

JArray

[0] = JObject
    [0] = JProperty
        [0] = JValue John Vandeberg
    [1] = JProperty
        [0] = JValue
    [2] = JProperty
        [0] = JValue
    [3] = JProperty
        [0] = JValue 3
    [4] = JProperty
        [0] = JValue 4/05/2012 2:32:00 PM

问题:为什么我无法清除\“转义字符,因为我认为这就是Deserialize没有给出应该包含项目名称的期望输出的原因。

2 个答案:

答案 0 :(得分:1)

你应该更好地创建一个class Object,以便更好地使用,例如Object就像这样:

public class Book
{
    public string Author { get; set; }
    public DateTime? ClosedDate { get; set; }
    public string Comment { get; set; }
    public int ComVal { get; set; }
    public DateTime? CreatedDate { get; set; }
    public string Description { get; set; }
    public string DType { get; set; }
    public DateTime? DueDate {get;set;}
    public int ID {get;set;}
    public string Name {get;set;}
    public string OtherID {get;set;}
    public float PercentComplete {get;set;}
    public int Priority {get;set;}
    public string PriorityName {get;set;}
    public int ProjectRef {get;set;}
    public string ResID {get;set;}
    public string ResName {get;set;}
    public string Responsible {get;set;}
    public int Status {get;set;}
    public string StatusName {get;set;}
    public string UpdateBy {get;set;}
    public DateTime? UpdateDate {get;set;}
}

然后,您可以DeserializeObject发送到已知的JavaScriptSerializer string json = "[{\"Author\":\"John Vandeberg\",\"ClosedDate\":null,\"Comment\":\"\",\"ComVal\":3,\"CreatedDate\":\"2012-05-04T14:32:00\",\"Description\":\"\",\"DType\":null,\"DueDate\":\"2012-06-20T00:00:00\",\"ID\":29,\"Name\":\"Review the detailed project management plan\",\"OtherID\":null,\"PercentComplete\":0,\"Priority\":142,\"PriorityName\":\"High\",\"ProjectRef\":43,\"ResID\":null,\"ResName\":null,\"Responsible\":\"John Vandeberg\",\"Status\":138,\"StatusName\":\"Open\",\"UpdateBy\":null,\"UpdateDate\":null},{\"Author\":\"John Vandeberg\",\"ClosedDate\":null,\"Comment\":\"\",\"ComVal\":1,\"CreatedDate\":\"2012-05-04T14:33:00\",\"Description\":\"Add Description\",\"DType\":null,\"DueDate\":\"2013-06-28T00:00:00\",\"ID\":31,\"Name\":\"Discuss recruitment with HR for required BA's\",\"OtherID\":null,\"PercentComplete\":0,\"Priority\":142,\"PriorityName\":\"High\",\"ProjectRef\":43,\"ResID\":null,\"ResName\":null,\"Responsible\":\"John Vandeberg\",\"Status\":138,\"StatusName\":\"Open\",\"UpdateBy\":\"John Vandeberg\",\"UpdateDate\":\"2012-05-07T21:25:00\"},{\"Author\":\"John Vandeberg\",\"ClosedDate\":null,\"Comment\":\"Test1\",\"ComVal\":1,\"CreatedDate\":\"2012-03-26T10:37:00\",\"Description\":\"PO Approved\",\"DType\":null,\"DueDate\":\"2013-06-28T00:00:00\",\"ID\":20,\"Name\":\"Get standard documentation from PMO 1\",\"OtherID\":null,\"PercentComplete\":2,\"Priority\":142,\"PriorityName\":\"High\",\"ProjectRef\":43,\"ResID\":null,\"ResName\":null,\"Responsible\":\"John Vandeberg\",\"Status\":138,\"StatusName\":\"Open\",\"UpdateBy\":\"John Vandeberg\",\"UpdateDate\":\"2012-04-07T23:29:00\"},{\"Author\":\"John Vandeberg\",\"ClosedDate\":null,\"Comment\":\"\",\"ComVal\":1,\"CreatedDate\":\"2012-05-04T14:30:00\",\"Description\":\"\",\"DType\":null,\"DueDate\":\"2013-06-28T00:00:00\",\"ID\":27,\"Name\":\"Schedule meeting with senior stakeholders for scope clarification\",\"OtherID\":null,\"PercentComplete\":0,\"Priority\":142,\"PriorityName\":\"High\",\"ProjectRef\":43,\"ResID\":null,\"ResName\":null,\"Responsible\":\"John Vandeberg\",\"Status\":138,\"StatusName\":\"Open\",\"UpdateBy\":null,\"UpdateDate\":null},{\"Author\":\"John Vandeberg\",\"ClosedDate\":null,\"Comment\":\"\",\"ComVal\":2,\"CreatedDate\":\"2012-05-04T14:31:00\",\"Description\":\"\",\"DType\":null,\"DueDate\":\"2013-07-31T00:00:00\",\"ID\":28,\"Name\":\"Review previous PCB minutes and provide feedback\",\"OtherID\":null,\"PercentComplete\":0,\"Priority\":142,\"PriorityName\":\"High\",\"ProjectRef\":43,\"ResID\":null,\"ResName\":null,\"Responsible\":\"John Vandeberg\",\"Status\":138,\"StatusName\":\"Open\",\"UpdateBy\":\"John Vandeberg\",\"UpdateDate\":\"2013-09-27T21:10:00\"}]"; var result = new JavaScriptSerializer().Deserialize<List<Book>>(json); 这样

Deserialize<List<Book>>

请注意DeserializeJSON Object Type List<Book>已知List<T> JSON

  • Object因为您的Book返回多个Object
  • Author因为它是result
  • 的类名

然后,如果您需要使用string Author = result[i].Author; // i > the index in the list (with your JSON, it's 0 - 4 because you have 5 results 中的Object,则可以使用此

ClosedDate

还在public DateTime? ClosedDate { get; set; }的{​​{1}}声明中注明,它被写为null,因为您为其返回值?因此,您需要添加null以接受类型DateTime

的{{1}}值

答案 1 :(得分:0)

转义字符是C#语法的一部分,无法使用&#34替换它们;替换&#34;功能。转义字符的目的是启动字符序列,这些字符序列必须与不带前缀转义字符的相同字符进行不同的解释。

不要对已解码的JSON字符串执行替换操作。

如果您对System.Web.Helpers(MVCApplication Solution Path \ packages \ Microsoft.AspNet.WebPages.2.0.30506.0 \ lib \ net40 \ System.Web.Helpers.dll)程序集没问题,那么使用JSON.Decode方法反序列化JSON对象。