Convert JSON String to JSON object in C#
This post will discuss how to convert a JSON String to a JSON object in C#.
1. Using JsonSerializer.Deserialize() method
For .NET versions 4.7.2 and later, you can use the JsonSerializer.Deserialize() method for deserializing a JSON string. It parses the specified JSON string into a specified .NET type.
The following example illustrates. Note that the JsonSerializer class is included in the System.Text.Json namespace.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Text.Json; public class Customer { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return $"[{Name}, {Age}]"; } } public class Example { public static void Main() { string jsonString = "[{\"Name\":\"Chris\",\"Age\":25},{\"Name\":\"Jennifer\",\"Age\":20}]"; List<Customer> customersList = JsonSerializer.Deserialize<List<Customer>>(jsonString); foreach (Customer customer in customersList) { Console.WriteLine(customer); } } } |
2. Using Json.NET Library
Before .NET Framework 4.7.2, you can use the Json.NET library for deserializing a JSON. Its Newtonsoft.Json.JsonConvert.DeserializeObject() method accepts a JSON string to deserialize, and creates an instance of the object type, as specified by the generic parameter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Collections.Generic; using Newtonsoft.Json; public class Customer { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return $"[{Name}, {Age}]"; } } public class Example { public static void Main() { string jsonString = "[{\"Name\":\"Chris\",\"Age\":25},{\"Name\":\"Jennifer\",\"Age\":20}]"; List<Customer> customersList = JsonConvert.DeserializeObject<List<Customer>>(jsonString); foreach (Customer customer in customersList) { Console.WriteLine(customer); } } } |
If you don’t need a typed object, you can do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using Newtonsoft.Json; public class Example { public static void Main() { string jsonString = "[{\"Name\":\"Chris\",\"Age\":25},{\"Name\":\"Jennifer\",\"Age\":20}]"; dynamic customersList = JsonConvert.DeserializeObject(jsonString); Console.WriteLine(customersList); } } |
3. Using JavaScriptSerializer Class
For AJAX-enabled applications, you can use the JavaScriptSerializer class for deserialization of data objects. To deserialize a JSON string, you can use the Deserialize() or DeserializeObject() methods.
The following example provides a simple illustration of JavaScriptSerializer.DeserializeObject() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Collections.Generic; using System.Web.UI; using System.Web.Script.Serialization; public class Customer { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return $"[{Name}, {Age}]"; } } public class Example { public static void Main() { string jsonString = "[{\"Name\":\"Chris\",\"Age\":25},{\"Name\":\"Jennifer\",\"Age\":20}]"; JavaScriptSerializer serializer = new JavaScriptSerializer(); List<Customer> customersList = serializer.Deserialize<List<Customer>>(jsonString); foreach (Customer customer in customersList) { Console.WriteLine(customer); } } } |
That’s all about converting a JSON String to a JSON object in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)