如何反序列化具有动态(数字)键名的子对象?

How can I deserialize a child object with dynamic (numeric) key names?(如何反序列化具有动态(数字)键名的子对象?)
本文介绍了如何反序列化具有动态(数字)键名的子对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何反序列化此 JSON 数据?100034"等键本质上是动态的.

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

有没有一种方法可以直接访问具有名称、状态和 ID 的每个对象?

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

推荐答案

对于属性名称可变的 JSON 对象,您可以使用 Dictionary 代替常规类,其中 T 是表示项目数据的类.

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.

像这样声明你的类:

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

然后像这样反序列化:

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

演示:

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

输出:

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

这篇关于如何反序列化具有动态(数字)键名的子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)