{
"id" : 0,
"name" : "meeting",
"text" : "10 pm",
"location" : "Place1",
"startdate" : "10/27/2012 17:11",
"enddate" : "10/27/2012 18:41",
"description" : "Description",
"chairman" : "0",
"members" : [2, 1],
"messagetype" : {
"SMS" : false,
"Email" : true
},
"smsmessage" : null,
"emailmessage" : "this is message",
"emailsubject" : null,
"reminder" : "5",
"timetosendemail" : "10/23/2012 00:00",
"timetosendsms" : null
}
這是我的json字符串。我需要的是解析這個字符串並將每個值存儲到一個類的特定成員中。解析並將JSON字符串存儲到使用C的類中#
類是這樣
public class Event
{
public int id { get; set; }
public string name {get;set;}
public string text { get; set; }
public DateTime start_date { get; set; }
public DateTime end_date { get; set; }
public string location { get; set; }
public double ReminderAlert { get; set; }
public MessagerType MessageType { get; set; }
public string SmsMessage { get; set; }
public string EmailMessage { get; set; }
public string EmailSubject { get; set; }
public DateTime TimeToSendEmail { get; set; }
public DateTime TimeToSendSMS { get; set; }
public string[] members {get;set;}
}
我用Json.net庫parse..and下面
var eventValues = JsonConvert.DeserializeObject<List<Dictionary<string, Event>>>(stringEvent);
我的代碼片段給出運行此代碼我得到這個錯誤後
無法將當前JSON對象(例如{「name」:「value」})反序列化爲類型'System.Collections.Generic.List
1[System.Collections.Generic.Dictionary
2 [System.String,CalendarScheduler.Models.Event]]',因爲該類型需要JSON數組(例如, [1,2,3])來正確地反序列化。要修復此錯誤,請將JSON更改爲JSON數組(例如[1,2,3])或更改反序列化類型,以使其成爲正常的.NET類型(例如,不是像整數這樣的基本類型,不是像數組或List的集合類型),可以從JSON對象反序列化。 JsonObjectAttribute也可以添加到類型中,以強制它從JSON對象反序列化。
路徑 '身份證',1號線,6位
應該怎樣做,以避免這種例外...?
我看到JSON在類'start_date',但'startdate',同樣的結束日期。確保命名是一致的。 – Oded
而'10/23/2012 00:00'是Json不是有效的DateTime字符串。您可能需要一個自定義日期時間轉換器。 –
此外,TimeToSendSms的C#Model屬性是DateTime,因爲您發送空值,所以一些JSON序列化程序將引發錯誤。 – Leniency