<i id='gxlGY'><tr id='gxlGY'><dt id='gxlGY'><q id='gxlGY'><span id='gxlGY'><b id='gxlGY'><form id='gxlGY'><ins id='gxlGY'></ins><ul id='gxlGY'></ul><sub id='gxlGY'></sub></form><legend id='gxlGY'></legend><bdo id='gxlGY'><pre id='gxlGY'><center id='gxlGY'></center></pre></bdo></b><th id='gxlGY'></th></span></q></dt></tr></i><div id='gxlGY'><tfoot id='gxlGY'></tfoot><dl id='gxlGY'><fieldset id='gxlGY'></fieldset></dl></div>
    <tfoot id='gxlGY'></tfoot>

  1. <small id='gxlGY'></small><noframes id='gxlGY'>

      • <bdo id='gxlGY'></bdo><ul id='gxlGY'></ul>
      <legend id='gxlGY'><style id='gxlGY'><dir id='gxlGY'><q id='gxlGY'></q></dir></style></legend>
    1. 将嵌套的 JSON 反序列化为 C# 对象

      时间:2023-08-24
      <i id='cwdk0'><tr id='cwdk0'><dt id='cwdk0'><q id='cwdk0'><span id='cwdk0'><b id='cwdk0'><form id='cwdk0'><ins id='cwdk0'></ins><ul id='cwdk0'></ul><sub id='cwdk0'></sub></form><legend id='cwdk0'></legend><bdo id='cwdk0'><pre id='cwdk0'><center id='cwdk0'></center></pre></bdo></b><th id='cwdk0'></th></span></q></dt></tr></i><div id='cwdk0'><tfoot id='cwdk0'></tfoot><dl id='cwdk0'><fieldset id='cwdk0'></fieldset></dl></div>

          <bdo id='cwdk0'></bdo><ul id='cwdk0'></ul>

                <tbody id='cwdk0'></tbody>
              <tfoot id='cwdk0'></tfoot>
              <legend id='cwdk0'><style id='cwdk0'><dir id='cwdk0'><q id='cwdk0'></q></dir></style></legend>

                <small id='cwdk0'></small><noframes id='cwdk0'>

                本文介绍了将嵌套的 JSON 反序列化为 C# 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我从如下所示的 API 获取 JSON:

                I am getting JSON back from an API that looks like this:

                {
                  "Items": {
                    "Item322A": [{
                      "prop1": "string",
                      "prop2": "string",
                      "prop3": 1,
                      "prop4": false
                    },{
                      "prop1": "string",
                      "prop2": "string",
                      "prop3": 0,
                      "prop4": false
                    }],
                       "Item2B": [{
                      "prop1": "string",
                      "prop2": "string",
                      "prop3": 14,
                      "prop4": true
                    }]
                  },
                  "Errors": ["String"]
                }
                

                我已经尝试了几种方法来在 c# 对象中表示这个 JSON(这里列出的太多了).我已经尝试过使用列表和字典,这是我最近尝试表示它的示例:

                I have tried a few approaches to represent this JSON in c# objects (too many to list here). I've tried with lists and dictionaries, here is a recent example of how I've tried to represent it:

                    private class Response
                    {
                        public Item Items { get; set; }
                        public string[] Errors { get; set; }
                    }
                
                    private class Item
                    {
                        public List<SubItem> SubItems { get; set; }
                    }
                
                    private class SubItem
                    {
                        public List<Info> Infos { get; set; }
                    }
                
                    private class Info
                    {
                        public string Prop1 { get; set; }
                        public string Prop2 { get; set; }
                        public int Prop3 { get; set; }
                        public bool Prop4 { get; set; }
                    }
                

                这是我用来反序列化 JSON 的方法:

                And here is the method I am using to deserialize the JSON:

                    using (var sr = new StringReader(responseJSON))
                    using (var jr = new JsonTextReader(sr))
                    {
                        var serial = new JsonSerializer();
                        serial.Formatting = Formatting.Indented;
                        var obj = serial.Deserialize<Response>(jr);
                    }
                

                obj 包含 ItemsErrors.Items 包含 SubItems,但 SubItemsnull.因此,除了 Errors 实际上没有任何东西被反序列化.

                obj contains Items and Errors. And Items contains SubItems, but SubItems is null. So nothing except for Errors is actually getting deserialized.

                应该很简单,但由于某种原因我无法找出正确的对象表示

                It should be simple, but for some reason I can't figure out the correct object representation

                推荐答案

                "Items"使用Dictionary>,即:

                class Response
                {
                    public Dictionary<string, List<Info>> Items { get; set; }
                    public string[] Errors { get; set; }
                }
                
                class Info
                {
                    public string Prop1 { get; set; }
                    public string Prop2 { get; set; }
                    public int Prop3 { get; set; }
                    public bool Prop4 { get; set; }
                }
                

                这假定项目名称 "Item322A""Item2B" 会因响应而异,并将这些名称作为字典键读取.

                This assumes that the item names "Item322A" and "Item2B" will vary from response to response, and reads these names in as the dictionary keys.

                示例小提琴.

                这篇关于将嵌套的 JSON 反序列化为 C# 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在反序列化期间以编程方式选择构造函数? 下一篇:在 .net core 3 中将 newtonsoft 代码转换为 System.Text

                相关文章

                最新文章

                <small id='l7MH3'></small><noframes id='l7MH3'>

                1. <legend id='l7MH3'><style id='l7MH3'><dir id='l7MH3'><q id='l7MH3'></q></dir></style></legend>
                2. <i id='l7MH3'><tr id='l7MH3'><dt id='l7MH3'><q id='l7MH3'><span id='l7MH3'><b id='l7MH3'><form id='l7MH3'><ins id='l7MH3'></ins><ul id='l7MH3'></ul><sub id='l7MH3'></sub></form><legend id='l7MH3'></legend><bdo id='l7MH3'><pre id='l7MH3'><center id='l7MH3'></center></pre></bdo></b><th id='l7MH3'></th></span></q></dt></tr></i><div id='l7MH3'><tfoot id='l7MH3'></tfoot><dl id='l7MH3'><fieldset id='l7MH3'></fieldset></dl></div>

                  <tfoot id='l7MH3'></tfoot>

                    • <bdo id='l7MH3'></bdo><ul id='l7MH3'></ul>