Nodoid
Nodoid

Reputation: 1581

Deserialising json string to List<T>

I have a web service that is outputting JSON in the form

{"AppointmentList":[{"AppointmentList":{"id":"1","MeetingId":"1","MeetingName":"Test Meeting 1","Length":"90","Room":"B2C","DateTimeFrom":"1st Sept 2016","Venue":"The old pub","DateCreated":"2016-08-30 00:00:00","DateDue":"2016-09-01 00:00:00","UserId":"JohnsonPa"}},{"AppointmentList":{"id":"2","MeetingId":"2","MeetingName":"Test Meeting 2","Length":"60","Room":"B2C","DateTimeFrom":"11th Sept 2016","Venue":"The old pub","DateCreated":"2016-09-01 00:00:00","DateDue":"2016-09-12 00:00:00","UserId":"JohnsonPa"}...}]}

I am trying to deserialise this in to List. Normally, I would have a Base Class that would contain a property List AppointmentList {get; set;}, however, that would mean that I can't use type T and need a pile of duplicate code for each class.

I can certainly create BaseClass with a property public List Data {get; set;} however, as the JSON won't deserialise to Data (incorrect name) and the JSON PropertyName can't be set to the class name derived from typeof(T).ToString().

Is there a way to achieve what I'm trying to do without resorting to lots of code duplication?

I've tried casting the deserialiser to JArray and creating a reader from that, but this throws an exception.

Upvotes: 1

Views: 109

Answers (3)

Filip Cordas
Filip Cordas

Reputation: 2561

What is wrong with generics? If you want a schemaless data structure use JObject or dynamic if not you can try this.

 class Program
    {
        public const string json = @"{""AppointmentList"":[{""AppointmentList"":{""id"":""1"",""MeetingId"":""1"",""MeetingName"":""Test Meeting 1"",""Length"":""90"",""Room"":""B2C"",""DateTimeFrom"":""1st Sept 2016"",""Venue"":""The old pub"",""DateCreated"":""2016-08-30 00:00:00"",""DateDue"":""2016-09-01 00:00:00"",""UserId"":""JohnsonPa""}},{""AppointmentList"":{""id"":""2"",""MeetingId"":""2"",""MeetingName"":""Test Meeting 2"",""Length"":""60"",""Room"":""B2C"",""DateTimeFrom"":""11th Sept 2016"",""Venue"":""The old pub"",""DateCreated"":""2016-09-01 00:00:00"",""DateDue"":""2016-09-12 00:00:00"",""UserId"":""JohnsonPa""}}]}";

        static void Main(string[] args)
        {
            var items = Newtonsoft.Json.JsonConvert.DeserializeObject<AppointmentItemList<Meeting1>>(json).GetList();

            var items2 = Newtonsoft.Json.JsonConvert.DeserializeObject<AppointmentItemList<Meeting2>>(json).GetList();

            Console.ReadLine();
        }

        public class AppointmentItemList<T> 
        {
            public List<AppointmentItem> AppointmentList { get; set; }

            public class AppointmentItem 
            {
                public T AppointmentList { get; set; }
            }

            public IList<T> GetList()
            {
                return AppointmentList.Select(al => al.AppointmentList).ToList();
            }
        }

        public class Meeting1 
        {
            [Newtonsoft.Json.JsonProperty("id")]
            public string Id { get; set; }

            public string MeetingName { get; set; }
        }

        public class Meeting2
        {
            [Newtonsoft.Json.JsonProperty("id")]
            public string Id { get; set; }

            public string Room { get; set; }
        }
    }

Upvotes: 0

mybirthname
mybirthname

Reputation: 18127

Here how to convert it to List<object>

dynamic data = JsonConvert.DeserializeObject(json);
JArray array = data.AppointmentList;

List<object> objectList = array.ToObject<List<object>>();

Upvotes: 0

chrisc
chrisc

Reputation: 434

Im not sure if this is exactly what you need, but maybe something like this would work? It allows you to successfully deserialize to a JArray like you state you tried at the end of your question.

JArray result = JsonConvert.DeserializeObject<dynamic>(json).AppointmentList;

Upvotes: 1

Related Questions