C# Json Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To read and parse a JSON file in C#, you can use the JsonConvert
class provided by the Newtonsoft.Json
library.
Here is an example of how to read and parse a JSON file:
using Newtonsoft.Json; using System.IO; // Define a class to store the data public class Person { public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } } // Read the JSON file string json = File.ReadAllText("data.json"); // Parse the JSON string to a list of objects List<Person> people = JsonConvert.DeserializeObject<List<Person>>(json);
In the example above, we first define a class Person
to store the data we will parse from the JSON file. We then read the JSON file into a string variable json
, and parse it using the JsonConvert.DeserializeObject
method, which takes the JSON string and the type of object we want to parse it into.
The JsonConvert
class also provides other methods for deserializing JSON data, such as DeserializeObjectAsync
for asynchronous deserialization, and DeserializeAnonymousType
for deserializing JSON to an anonymous type.
To convert a JSON string to a custom class, simply replace the List<Person>
with your own custom class in the DeserializeObject
method.
To deserialize JSON values dynamically, you can use the JObject
or JArray
class provided by the Newtonsoft.Json.Linq
namespace. Here is an example:
using Newtonsoft.Json.Linq; // Read the JSON file string json = File.ReadAllText("data.json"); // Parse the JSON string to a dynamic object dynamic data = JObject.Parse(json); // Access the values dynamically string name = data["name"]; int age = data["age"];
In the example above, we use the JObject.Parse
method to parse the JSON string to a dynamic object, which we can then access using dynamic syntax.
To deserialize JSON to a simple dictionary, you can use the JsonConvert.DeserializeObject<Dictionary<string, object>>
method. Here is an example:
// Read the JSON file string json = File.ReadAllText("data.json"); // Parse the JSON string to a dictionary Dictionary<string, object> data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json); // Access the values string name = data["name"].ToString(); int age = int.Parse(data["age"].ToString());
In the example above, we use the JsonConvert.DeserializeObject
method with a Dictionary<string, object>
generic type parameter to parse the JSON string to a dictionary.
To parse JSON to an array, you can use the JsonConvert.DeserializeObject<T[]>
method. Here is an example:
// Read the JSON file string json = File.ReadAllText("data.json"); // Parse the JSON string to an array string[] data = JsonConvert.DeserializeObject<string[]>(json); // Access the values foreach (string item in data) { Console.WriteLine(item); }
JSON deserialization in C#:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; Person person = JsonConvert.DeserializeObject<Person>(jsonString);
Deserialize JSON object in C#:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; Person person = JsonConvert.DeserializeObject<Person>(jsonString);
Using Newtonsoft.Json for JSON deserialization in C#:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; Person person = JsonConvert.DeserializeObject<Person>(jsonString);
C# JSON deserialization example:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; Person person = JsonConvert.DeserializeObject<Person>(jsonString);
Deserialize JSON array in C#:
string jsonArray = "[{\"Name\":\"John\",\"Age\":30},{\"Name\":\"Jane\",\"Age\":25}]"; List<Person> people = JsonConvert.DeserializeObject<List<Person>>(jsonArray);
JSON deserialization using DataContractJsonSerializer in C#:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; Person person; using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person)); person = (Person)serializer.ReadObject(ms); }
C# deserialize JSON to object:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; Person person = JsonConvert.DeserializeObject<Person>(jsonString);
JSON deserialization with Json.NET in C#:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; Person person = JsonConvert.DeserializeObject<Person>(jsonString);
Deserialize JSON to dynamic object in C#:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; dynamic person = JsonConvert.DeserializeObject(jsonString);
Handle null values during JSON deserialization in C#:
JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; string jsonString = "{\"Name\":\"John\",\"Age\":null}"; Person person = JsonConvert.DeserializeObject<Person>(jsonString, settings);
JSON deserialization with System.Text.Json in C#:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; Person person = JsonSerializer.Deserialize<Person>(jsonString);
C# JSON deserialization error handling:
try { string jsonString = "{\"Name\":\"John\",\"Age\":\"Thirty\"}"; Person person = JsonConvert.DeserializeObject<Person>(jsonString); } catch (JsonException ex) { Console.WriteLine($"Error during deserialization: {ex.Message}"); }
JSON deserialization with anonymous types in C#:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; var person = JsonConvert.DeserializeAnonymousType(jsonString, new { Name = "", Age = 0 });
C# deserialize JSON to dictionary:
string jsonString = "{\"Name\":\"John\",\"Age\":30}"; Dictionary<string, object> personDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
Custom JSON deserialization in C#:
[JsonConverter(typeof(CustomConverter))] public class Person { public string Name { get; set; } public int Age { get; set; } }
Deserialize nested JSON objects in C#:
string jsonString = "{\"Person\":{\"Name\":\"John\",\"Age\":30}}"; Wrapper wrapper = JsonConvert.DeserializeObject<Wrapper>(jsonString);