Problem
These are some of the classes I teach:
class MyDate
{
int year, month, day;
}
class Lad
{
string firstName;
string lastName;
MyDate dateOfBirth;
}
And I’d like to convert a Lad object to a JSON string that looks like this:
{
"firstName":"Markoff",
"lastName":"Chaney",
"dateOfBirth":
{
"year":"1901",
"month":"4",
"day":"30"
}
}
(Except for the formatting.) This link was located, but it uses a namespace that isn’t available in.NET 4. JSON.NET was also mentioned, but their website appears to be down right now, and I’m not a fan of using external DLL files.
Other than manually developing a JSON string writer, are there any other options?
Asked by Hui
Solution #1
We all enjoy one-liners, so
…this one relies on the Newtonsoft NuGet package, which is a popular and superior serializer to the default.
Newtonsoft.Json.JsonConvert.SerializeObject(new {foo = "bar"})
Serializing and Deserializing JSON (Documentation)
Answered by mschmoock
Solution #2
JavaScriptSerializer is not recommended by Microsoft.
See the documentation page’s header:
You might use the JavaScriptSerializer class (add System.Web.Extensions as a reference):
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);
}
}
Answered by Darin Dimitrov
Solution #3
Use the Json.Net library, which may be found in Nuget Packet Manager.
Json String Serialization:
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 );
Answered by Gokulan P H
Solution #4
MSDN1 and MSDN2 both use the DataContractJsonSerializer class.
My example: HERE.
Unlike JavaScriptSerializer, it can securely deserialize objects from a JSON string. However, I continue to prefer Json.NET.
Answered by Edgar
Solution #5
In the System, a new JSON serializer is accessible. Namespace Text.Json It’s part of the.NET Core 3.0 common framework, and it’s also available as a NuGet package for projects using.NET Standard,.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);
}
}
The classes to be serialized in this example have properties rather than fields; the System. Text. Currently, the Json serializer does not serialize fields.
Documentation:
Answered by tdykstra
Post is based on https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net