Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am trying to understand json serialization. Now I am having a confusion regarding SerializeObject(object obj). When normal dot net string is passed to SerializeObject(), the resultant json contain "". But when a list of Custom type is passed to SerializeObject(List<t>), the resultant json string does not contain "". I am not able to understand the difference. Normally in dot net string the "" is not part of string as the value.

What I have tried:

Program.cs
----------
C#
string str= "[{ \"firstName\":\"Todd\",\"lastName\":\"Grover\",\"gender\":\"Male\",\"salary\":50000},{ \"firstName\":\"Sara\",\"lastName\":\"Baker\",\"gender\":\"Female\",\"salary\":40000}]"; 
    Console.WriteLine(str); 
    var js = JsonConvert.SerializeObject(str); 
    Console.WriteLine(js);

------------------------------------------------------------------------

    

Employee
--------

    public class Employee
        {
            public string firstName { get; set; }
            public string lastName { get; set; }
            public string gender { get; set; }
            public int salary { get; set; }
        }

Program.cs
----------
    var employee1 = new Employee
                {
                    firstName = "Todd",
                    lastName = "Grover",
                    gender = "Male",
                    salary = 50000
                };
    
                var employee2 = new Employee
                {
                    firstName = "Sara",
                    lastName = "Baker",
                    gender = "Female",
                    salary = 40000
                };
    
                var listEmployee = new List<employee>();
                listEmployee.Add(employee1);
                listEmployee.Add(employee2);
                var js = JsonConvert.SerializeObject(listEmployee);
                Console.WriteLine(js);
Posted
Updated 25-Aug-19 7:56am
v2

1 solution

0 you are wrong: the Json for the List will contain quotes ! however it will not contain the backslash escape character you see in the string..

1 assuming you can use JSON to de-serialize both the string, and the list of Employee ... without error ... what is the problem ?

2 to understand how Json escapes strings: [^]

3 i suggest you get in the habit of using the Json.Deserialize method with generic type constraint:

List<JsonTest.Employee> jslist = JsonConvert.DeserializeObject<List<JsonTest.Employee>>(jt.js2);

This will save you having to convert the 'object result to whatever Type, and, imho, make the intent of your code clearer.

imho, it's unfortunate Json doesn't have a parallel generic 'Serialize operator.

4 i suggest you explore serialization using C#'s DataContract facilities: imho much better than the Newtonsoft stuff: [^], [^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900