简体   繁体   中英

Parse and store json string into a class using C#

{
    "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 (eg {"name":"value"}) into type 'System.Collections.Generic.List 1[System.Collections.Generic.Dictionary 2[System.String,CalendarScheduler.Models.Event]]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg 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?

var eventValues = JsonConvert.DeserializeObject<Event>(stringEvent);

And consider Oded's comment. And match all JSON property names to C# property names.

Try this:

public T Deserialize<T>(string json)
{
    T obj = Activator.CreateInstance<T>();
    MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
    obj = (T)serializer.ReadObject(ms);
    ms.Close();
    return obj;
}

Event MyEvent = Deserialize<Event>(jsonString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM