1

I am trying to deserialize JSON into List. I have .NET 4.0. Normal deserialization into an object works fine. I am an absolute beginner in C#.

I wrote the following code with the help from various forums. When I am trying to load the deserialized JSON into a List for Lad type, it doesn't give any error but when I check Lads object it's just blank.

using System;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.Collections.Generic;

namespace RESTServicesJSONParserExample
{
    public class MyDate
    {
        public int year { get; set; }
        public int month { get; set; }
        public int day { get; set; }
    }

    public class Lad
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public MyDate dateOfBirth { get; set; }
    }

    class Program
    {
        static void Main()
        {
            var obj = new Lad
            {
                firstName = "Markoff",
                lastName = "Chaney",
                dateOfBirth = new MyDate
                {
                    year = 1901,
                    month = 4,
                    day = 30
                }

            };
            var json = new JavaScriptSerializer().Serialize(obj);
            Console.WriteLine(json);


            //This fails to load desereialized JSON in the List
            List<Lad> Lads = new JavaScriptSerializer().Deserialize<List<Lad>>(json);
            Lad Lad1 = new JavaScriptSerializer().Deserialize<Lad>(json);

            Console.WriteLine(Lad1.firstName);


            //Nothing returned here
            foreach (Lad Lad2 in Lads)
            {
                Console.WriteLine(Lad2.firstName);
            }

            Console.ReadLine();
        }
    }
}

2 Answers 2

2

You are serializing a single Lad. You can't deserialize it as a collection of Lads!

You could change the code in:

var obj = new List<Lad>
{ 
    new Lad
    {
        firstName = "Markoff",
        lastName = "Chaney",
        dateOfBirth = new MyDate
        {
            year = 1901,
            month = 4,
            day = 30
        }
    } 
};

Now Lads will have one Lad, while Lad1 will crash (because you can't deserialize a collection as a single element).

Just as a curiosity, your "original" json was

{"firstName":"Markoff","lastName":"Chaney","dateOfBirth":{"year":1901,"month":4,"day":30}}

Introducing the new List<> it becomes:

[{"firstName":"Markoff","lastName":"Chaney","dateOfBirth":{"year":1901,"month":4,"day":30}}]

Se the initial and final []? Now there is an array of objects in the json object!

Sign up to request clarification or add additional context in comments.

1 Comment

Worked! and thanks for the super quick response and explanation.
1

I actually just had to do this yesterday and this is how I did it (not sure if there's an easier way, but it worked for me). I deserialized each object and added that object to my List. I had a list of json objects (generic objects) so I looped through them with a foreach, adding them to the List. Also, I use JsonConvert.DeserializeObject, but I believe it should work the same way.Try this though:

List<Lad> Lads = new List<Lad>();

Lads.Add(new JavaScriptSerializer().Deserialize<Lad>(json));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.