To deserialize a JSON array (or list) in C# into a corresponding .NET data structure, you can use the System.Text.Json
or Newtonsoft.Json
libraries, depending on the .NET version you are working with. Both libraries provide easy-to-use methods for parsing JSON data.
Here are examples of how to deserialize a JSON array using both libraries:
using System; using System.Text.Json; class Program { static void Main() { string json = @"[1, 2, 3, 4, 5]"; // JSON array as a string // Deserialize the JSON array into a List<int> var integers = JsonSerializer.Deserialize<List<int>>(json); // Output the result foreach (int number in integers) { Console.WriteLine(number); } } }
using System; using Newtonsoft.Json; using System.Collections.Generic; class Program { static void Main() { string json = @"[1, 2, 3, 4, 5]"; // JSON array as a string // Deserialize the JSON array into a List<int> var integers = JsonConvert.DeserializeObject<List<int>>(json); // Output the result foreach (int number in integers) { Console.WriteLine(number); } } }
Both examples demonstrate how to deserialize a JSON array into a List<int>
containing the integers 1 to 5. You can replace the int
with other data types or custom classes to deserialize more complex JSON structures.
Note: If you are using .NET Core 3.0 or later, Microsoft recommends using System.Text.Json
for serialization and deserialization due to its performance and ease of use. However, if you are working with an older .NET version or you prefer to use Newtonsoft.Json (Newtonsoft.Json) for any reason, it is still a widely-used and reliable library for JSON processing in .NET.
"C# Deserialize JSON Array using JSON.NET"
using Newtonsoft.Json; using System; using System.Collections.Generic; var jsonArray = "[{\"Name\":\"John\",\"Age\":25},{\"Name\":\"Alice\",\"Age\":30}]"; var persons = JsonConvert.DeserializeObject<List<Person>>(jsonArray);
jsonArray
) into a list of C# objects (Person
) using JSON.NET's JsonConvert.DeserializeObject
method."C# Deserialize JSON Array with Anonymous Type"
using Newtonsoft.Json; using System; var jsonArray = "[{\"Name\":\"John\",\"Age\":25},{\"Name\":\"Alice\",\"Age\":30}]"; var persons = JsonConvert.DeserializeAnonymousType(jsonArray, new[] { new { Name = "", Age = 0 } });
jsonArray
) into an array of anonymous types using JSON.NET's JsonConvert.DeserializeAnonymousType
method."C# Deserialize JSON Array with Dynamic Object"
using Newtonsoft.Json; using System; var jsonArray = "[{\"Name\":\"John\",\"Age\":25},{\"Name\":\"Alice\",\"Age\":30}]"; dynamic persons = JsonConvert.DeserializeObject(jsonArray);
jsonArray
) into a dynamic object using JSON.NET, allowing access to properties without a predefined class structure."C# Deserialize JSON Array with LINQ to JSON"
using Newtonsoft.Json.Linq; using System; var jsonArray = "[{\"Name\":\"John\",\"Age\":25},{\"Name\":\"Alice\",\"Age\":30}]"; var persons = JArray.Parse(jsonArray).ToObject<List<Person>>();
jsonArray
) using Newtonsoft.Json.Linq.JArray
and converts it into a list of C# objects (Person
)."C# Deserialize JSON Array with System.Text.Json"
using System.Text.Json; using System; var jsonArray = "[{\"Name\":\"John\",\"Age\":25},{\"Name\":\"Alice\",\"Age\":30}]"; var persons = JsonSerializer.Deserialize<List<Person>>(jsonArray);
jsonArray
) into a list of C# objects (Person
) using the System.Text.Json.JsonSerializer
class."C# Deserialize JSON Array with Custom Converter"
using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; public class CustomListConverter<T> : JsonConverter<List<T>> { public override List<T> ReadJson(JsonReader reader, Type objectType, List<T> existingValue, bool hasExistingValue, JsonSerializer serializer) { return serializer.Deserialize<List<T>>(reader); } public override void WriteJson(JsonWriter writer, List<T> value, JsonSerializer serializer) { serializer.Serialize(writer, value); } } var jsonArray = "[{\"Name\":\"John\",\"Age\":25},{\"Name\":\"Alice\",\"Age\":30}]"; var persons = JsonConvert.DeserializeObject<List<Person>>(jsonArray, new CustomListConverter<Person>());
jsonArray
) into a list of C# objects (Person
) using a custom converter with JSON.NET."C# Deserialize JSON Array with Error Handling"
using Newtonsoft.Json; using System; var jsonArray = "[{\"Name\":\"John\",\"Age\":25},{\"Name\":\"Alice\",\"Age\":\"InvalidAge\"}]"; try { var persons = JsonConvert.DeserializeObject<List<Person>>(jsonArray); } catch (JsonException ex) { Console.WriteLine($"Error during deserialization: {ex.Message}"); }
"C# Deserialize JSON Array with Custom Property Names"
using Newtonsoft.Json; using System; var jsonArray = "[{\"full_name\":\"John Doe\",\"user_age\":25},{\"full_name\":\"Alice Smith\",\"user_age\":30}]"; var persons = JsonConvert.DeserializeObject<List<Person>>(jsonArray, new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() } });
jsonArray
) with custom property names into a list of C# objects (Person
) using a custom contract resolver with JSON.NET."C# Deserialize JSON Array with Type Information"
using Newtonsoft.Json; using System; var jsonArray = "[{\"$type\":\"Namespace.Person, Assembly\",\"Name\":\"John\",\"Age\":25}]"; var persons = JsonConvert.DeserializeObject<List<Person>>(jsonArray, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
jsonArray
) with type information into a list of C# objects (Person
) using type name handling with JSON.NET."C# Deserialize JSON Array with Nullable Types"
using Newtonsoft.Json; using System; var jsonArray = "[{\"Name\":\"John\",\"Age\":25},{\"Name\":\"Alice\",\"Age\":null}]"; var persons = JsonConvert.DeserializeObject<List<Person>>(jsonArray);
jsonArray
) with nullable types into a list of C# objects (Person
) using JSON.NET.setbackground bitmask enum-flags cluster-analysis state syntastic for-loop fileinputstream adb http-proxy