How do I turn a C# object into a JSON string in .NET?

ID : 1219

viewed : 243

Tags : c#.netjsonserializationc#





Top 5 Answer for How do I turn a C# object into a JSON string in .NET?

vote vote

100

Since we all love one-liners

... this one depends on the Newtonsoft NuGet package, which is popular and better than the default serializer.

Newtonsoft.Json.JsonConvert.SerializeObject(new {foo = "bar"}) 

Documentation: Serializing and Deserializing JSON

vote vote

84

Please Note

Microsoft recommends that you DO NOT USE JavaScriptSerializer

See the header of the documentation page:

For .NET Framework 4.7.2 and later versions, use the APIs in the System.Text.Json namespace for serialization and deserialization. For earlier versions of .NET Framework, use Newtonsoft.Json.


Original answer:

You could use the JavaScriptSerializer class (add reference to System.Web.Extensions):

using System.Web.Script.Serialization; 
var json = new JavaScriptSerializer().Serialize(obj); 

A full example:

using System; using System.Web.Script.Serialization;  public class MyDate {     public int year;     public int month;     public int day; }  public class Lad {     public string firstName;     public string lastName;     public MyDate dateOfBirth; }  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);     } } 
vote vote

74

Use Json.Net library, you can download it from Nuget Packet Manager.

Serializing to Json String:

 var obj = new Lad         {             firstName = "Markoff",             lastName = "Chaney",             dateOfBirth = new MyDate             {                 year = 1901,                 month = 4,                 day = 30             }         };  var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj); 

Deserializing to Object:

var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Lad>(jsonString ); 
vote vote

70

Use the DataContractJsonSerializer class: MSDN1, MSDN2.

My example: HERE.

It can also safely deserialize objects from a JSON string, unlike JavaScriptSerializer. But personally I still prefer Json.NET.

vote vote

59

A new JSON serializer is available in the System.Text.Json namespace. It's included in the .NET Core 3.0 shared framework and is in a NuGet package for projects that target .NET Standard or .NET Framework or .NET Core 2.x.

Example code:

using System; using System.Text.Json;  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 lad = new Lad         {             FirstName = "Markoff",             LastName = "Chaney",             DateOfBirth = new MyDate             {                 year = 1901,                 month = 4,                 day = 30             }         };         var json = JsonSerializer.Serialize(lad);         Console.WriteLine(json);     } } 

In this example the classes to be serialized have properties rather than fields; the System.Text.Json serializer currently doesn't serialize fields.

Documentation:

Top 3 video Explaining How do I turn a C# object into a JSON string in .NET?







Related QUESTION?