{
"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
}
This is my json string. What i need is to parse this string and store each values into specific members of a class.
The class is like this
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;}
}
I used Json.net library to parse..and my code snippet is given below
var eventValues = JsonConvert.DeserializeObject<List<Dictionary<string, Event>>>(stringEvent);
After running this code i get this error
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type ‘System.Collections.Generic.List
1[System.Collections.Generic.Dictionary2[System.String,CalendarScheduler.Models.Event]]’ because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path ‘id’, line 1, position 6.
What should be done to avoid this exception…?
This does not work?
And consider Oded’s comment. And match all JSON property names to C# property names.