advertise with us

How to Convert JSON Array to List in C#

Working with JSON data is a common task in modern applications, and often you’ll encounter JSON arrays that need to be converted into C# lists for easy manipulation. In this guide, we’ll demonstrate how to convert a JSON array into a List<T> in C# using Newtonsoft.Json.


Installing Newtonsoft.Json

Before you begin, ensure that the Newtonsoft.Json package is installed in your project. You can add it via the NuGet Package Manager:

dotnet add package Newtonsoft.Json

JSON Array Structure

Let’s assume the following JSON array:

[
{ "Id": 1, "Name": "Alice", "Age": 25 },
{ "Id": 2, "Name": "Bob", "Age": 30 },
{ "Id": 3, "Name": "Charlie", "Age": 35 }
]

We want to convert this JSON array into a list of objects in C#.


Step-by-Step Guide to Conversion

1. Define a Model Class

Create a C# class that matches the structure of the JSON objects:

public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

2. Deserialize JSON Array to List

Use the JsonConvert.DeserializeObject method to parse the JSON array and convert it into a List<Person>.

Code Example

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

class Program
{
static void Main()
{
string jsonArray = @"
[
{ ""Id"": 1, ""Name"": ""Alice"", ""Age"": 25 },
{ ""Id"": 2, ""Name"": ""Bob"", ""Age"": 30 },
{ ""Id"": 3, ""Name"": ""Charlie"", ""Age"": 35 }
]";

List<Person> people = JsonConvert.DeserializeObject<List<Person>>(jsonArray);

foreach (var person in people)
{
Console.WriteLine($"Id: {person.Id}, Name: {person.Name}, Age: {person.Age}");
}
}
}

public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

Output

Id: 1, Name: Alice, Age: 25
Id: 2, Name: Bob, Age: 30
Id: 3, Name: Charlie, Age: 35

Handling Nested JSON Arrays

If the JSON array contains nested objects, you’ll need to define additional classes. For example:

JSON with Nested Objects

[
{
"Id": 1,
"Name": "Alice",
"Age": 25,
"Address": { "Street": "123 Main St", "City": "New York" }
},
{
"Id": 2,
"Name": "Bob",
"Age": 30,
"Address": { "Street": "456 Elm St", "City": "Los Angeles" }
}
]

Updated Classes

public class Address
{
public string Street { get; set; }
public string City { get; set; }
}

public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Address Address { get; set; }
}

Code to Deserialize

List<Person> people = JsonConvert.DeserializeObject<List<Person>>(jsonArray);

foreach (var person in people)
{
Console.WriteLine($"Id: {person.Id}, Name: {person.Name}, Age: {person.Age}");
Console.WriteLine($"Address: {person.Address.Street}, {person.Address.City}");
}

Validating JSON Before Conversion

Always validate your JSON before deserializing to avoid runtime exceptions. Here’s a simple validation function:

static bool IsValidJson(string json)
{
try
{
JsonConvert.DeserializeObject(json);
return true;
}
catch
{
return false;
}
}

Conclusion

Converting JSON arrays to C# lists is straightforward with Newtonsoft.Json. By defining appropriate model classes and using the JsonConvert.DeserializeObject method, you can effortlessly parse JSON data into structured, strongly-typed lists. This allows for better manipulation and integration of JSON data in your applications.


Need Help with Your C# Projects?

We offer expert support and development services for projects of any size. Contact us for a free consultation and see how we can help you succeed.

CONTACT US NOW