Home > Backend Development > C++ > How to Deserialize JSON with Dynamic Child Object Keys in C#?

How to Deserialize JSON with Dynamic Child Object Keys in C#?

Mary-Kate Olsen
Release: 2025-01-07 12:39:42
Original
759 people have browsed it

How to Deserialize JSON with Dynamic Child Object Keys in C#?

Deserializing Child Objects with Dynamic Keys

In certain scenarios, you may encounter JSON data with dynamic keys for child objects. To deserialize this data effectively, consider using dictionaries instead of traditional classes.

Using Dictionaries for Deserialization

Instead of creating a traditional class for each child object, you can utilize a Dictionary to represent the data. Each key in the dictionary represents a dynamic key, and each value is an object of type T, which should represent the data structure of the child object.

Defining the Classes

For this example, define the classes as follows:

class RootObject
{
    public Dictionary<string, User> users { get; set; }
}

class User
{
    public string name { get; set; }
    public string state { get; set; }
    public string id { get; set; }
}
Copy after login

Deserializing the JSON Data

To deserialize the JSON data into your RootObject class, use the following code:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
Copy after login

Accessing Child Objects

Once deserialized, you can access the child objects directly through the dictionary key:

foreach (string key in root.users.Keys)
{
    User user = root.users[key];
    Console.WriteLine($"key: {key}");
    Console.WriteLine($"name: {user.name}");
    Console.WriteLine($"state: {user.state}");
    Console.WriteLine($"id: {user.id}");
    Console.WriteLine();
}
Copy after login

Example

Consider the following JSON data:

{
    "users" : {
        "100034" : {
            "name"  : "tom",
            "state" : "WA",
            "id"    : "cedf-c56f-18a4-4b1"
        },
        "10045" : {
            "name"  : "steve",
            "state" : "NY",
            "id"    : "ebb2-92bf-3062-7774"
        },
        "12345" : {
            "name"  : "mike",
            "state" : "MA",
            "id"    : "fb60-b34f-6dc8-aaf7"
        }
    }
}
Copy after login

Using the provided code, you can deserialize the data and access each child object, resulting in the following output:

key: 10045
name: steve
state: NY
id: ebb2-92bf-3062-7774

key: 12345
name: mike
state: MA
id: fb60-b34f-6dc8-aaf7

key: 100034
name: tom
state: WA
id: cedf-c56f-18a4-4b1
Copy after login

The above is the detailed content of How to Deserialize JSON with Dynamic Child Object Keys in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template