Before some time I have written a blog post – Converting a C# object into JSON string in that post one of reader Thomas Levesque commented that mostly people are using JSON.NET a popular high performance JSON for creating for .NET Created by James Newton- King. I agree with him if we are using .NET Framework 4.0 or higher version for earlier version still JavaScriptSerializer is good. So in this post we are going to learn How we can convert C# object into JSON string with JSON.NET framework.
What is JSON.NET:
JSON.NET is a very high performance framework compared to other serializer for converting C# object into JSON string. It is created by James Newton-Kind. You can find more information about this framework from following link.
http://james.newtonking.com/json
How to convert C# object into JSON string with JSON.NET framework:
For this I am going to use old application that I have used in previous post. Following is a employee class with two properties first name and last name.
I have created same object of “Employee” class as I have created in previous post like below.
Now it’s time to add JSON.NET Nuget package. You install Nuget package via following command.
I have installed like below.
Now we are done with adding NuGet package. Following is code I have written to convert C# object into JSON string.
Let's run application and following is a output as expected.
That’s it. It’s very easy. Hope you like it. Stay tuned for more.
What is JSON.NET:
JSON.NET is a very high performance framework compared to other serializer for converting C# object into JSON string. It is created by James Newton-Kind. You can find more information about this framework from following link.
http://james.newtonking.com/json
How to convert C# object into JSON string with JSON.NET framework:
For this I am going to use old application that I have used in previous post. Following is a employee class with two properties first name and last name.
public class Employee { public string FirstName { get; set; } public string LastName { get; set; } }
I have created same object of “Employee” class as I have created in previous post like below.
Employee employee=new Employee {FirstName = "Jalpesh", LastName = "Vadgama"};
Now it’s time to add JSON.NET Nuget package. You install Nuget package via following command.
I have installed like below.
Now we are done with adding NuGet package. Following is code I have written to convert C# object into JSON string.
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(employee); Console.WriteLine(jsonString);
Let's run application and following is a output as expected.
That’s it. It’s very easy. Hope you like it. Stay tuned for more.
Hello Jalpesh!
ReplyDeleteCan you write a post about Shallow/Deep copy of an object in C#?
Sure I will right in some time in future!!
DeleteHi Jalpesh,
ReplyDeleteI just want to know that is there any performance differences between newtonsoft.json and normal javascript serializer ????
Yes JSON.NET is faster then normal JavaScript serializer. You can find performance comparison of various JSON serializer from following link-http://theburningmonk.com/2012/08/performance-test-json-serializers-part-iii/
Delete