Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

c# - How can I deserialize a child object with dynamic (numeric) key names?

How can I deserialize this JSON data? The keys "100034" etc. are dynamic in nature.

{
    "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"
        }
    }
}

Is there a way I can directly access each object having name, state and Id?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For JSON objects having property names which can vary, you can use a Dictionary<string, T> in place of a regular class, where T is a class representing the item data.

Declare your classes like this:

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; }
}

Then deserialize like this:

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

Demo:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""users"": {
                ""10045"": {
                    ""name"": ""steve"",
                    ""state"": ""NY"",
                    ""id"": ""ebb2-92bf-3062-7774""
                },
                ""12345"": {
                    ""name"": ""mike"",
                    ""state"": ""MA"",
                    ""id"": ""fb60-b34f-6dc8-aaf7""
                },
                ""100034"": {
                    ""name"": ""tom"",
                    ""state"": ""WA"",
                    ""id"": ""cedf-c56f-18a4-4b1""
                }
            }
        }";

        RootObject root = JsonConvert.DeserializeObject<RootObject>(json);

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...