2012-03-15 155 views
0

我已经发布了如何反序列化在C#中的json但这是我的问题:C#:asp.net 3.5:反序列化JSON - json字符串或对象?

我得到这个JSON字符串:

data = @"[{""ShiftID"":""2"",""EmpName"":""dsdsfs""},{""ShiftID"":""4"",""EmpName"":""dsdsfd""}]"; 

和我的代码可以完全反序列化,但很好,如果我收到这种类型:

data = @"[{ 
    "Q6Option": null, 
    "Q5Comments": "", 
    "EndDate": "01/17/2012 12:57", 
    "Q4Comments": "", 
    "Active": 1, 
    "Q3Comments": "", 
    "Q8Option": null, 
    "Q2Comments": "", 
    "StartDate": "01/17/2012 12:57", 
    "Q3Option": null, 
    "Q5Option": null, 
    "Q1Comments": "", 
    "Q2Option": null, 
    "Station": "B1", 
    "Q7Option": null, 
    "SyncDate": null, 
    "ID": 1, 
    "Q8Comments": "", 
    "Q4Option": null, 
    "ShiftID": 1, 
    "EmpName": "CSM FName", 
    "Q1Option": null, 
    "Q7Comments": "", 
    "SyncStatus": 0, 
    "Q6Comments": "", 
    "EmpID": "111", 
    "createdAt": "2012-01-17T20:58:17.956Z", 
    "updatedAt": "2012-01-17T20:58:17.956Z", 
    "objectId": "XERFM5KrGv" 
}]" 

它不工作,因为那里缺少“” ..

那么如果你有一个想法,谢谢!

+1

问题是什么? – 2012-03-15 03:03:16

+0

你使用哪个工具来反序列化/序列化? – Mathieu 2012-03-15 03:13:41

+0

问题可能是由于'StartDate' /'EndDate'值 - .NET中的JSON串行器期望以特定的方式格式化日期/时间! – VinayC 2012-03-15 05:06:59

回答

0

您可以使用从.NET框架支持的JavaScriptSerializer类3.5

参考链接:

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

样本链接:

http://msdn.microsoft.com/en-us/library/bb412170.aspx

有一些样品json文本与您的问题相同:

{“__type”:“Shape:#MyApp.Shapes”,“x”:50,“y”:70}

+0

我试过了,但我不能把这个JSON字符串放入一个var或一个字符串中,它不起作用......它需要在每个属性前面加上“”...... – user181248 2012-03-15 22:40:18