首页 > 解决方案 > 在 C# 中从 Datatable 创建一个深层嵌套对象

问题描述

我已经尝试在 c# 中实现这一点并成功创建了子元素,但我不知道如何将子元素放入 Parent。

下面是我在数据表中获得的数据图像

您可以在以下链接中检查 Json 结构:

JSON 查看器

{
  "id": "RootLevelId",
  "name": "Target",
  "attributes": [
    {
      "id": "9C459435-3275-4CA1-B8FF-38DBB134EB20",
      "name": "City",
      "odinVariable": "qCity",
      "isSelectionOptional": "False",
      "levels": [
        {
          "id": "29667712-EAF4-4F52-96E6-DCD9BAE9A901",
          "name": "Amsterdam",
          "attributes": [
            {
              "id": "EE490334-930B-4A25-B421-0CE30947A5E2",
              "name": "Gender",
              "odinVariable": "qGender",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "8720372C-E14A-4E1B-A34C-1A45ED372303",
                  "name": "Male",
                  "attributes": [

                  ],
                  "target": 180,
                  "grossTarget": 180
                },
                {
                  "id": "8A9E75DE-FDE9-4234-A302-257A69A88564",
                  "name": "Female",
                  "attributes": [

                  ],
                  "target": 180,
                  "grossTarget": 180
                }
              ]
            },
            {
              "id": "0AC7D90E-2C14-4407-9C45-6BD0EB7FF061",
              "name": "Age",
              "odinVariable": "qAge",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "7B7FFA50-5EB2-4D68-A498-6D3EFB8911E2",
                  "name": "0 - 18",
                  "attributes": [

                  ],
                  "target": 90,
                  "grossTarget": 90
                },
                {
                  "id": "97C71B48-A892-46B9-AA06-7B92374AEEF9",
                  "name": "19 - 36",
                  "attributes": [

                  ],
                  "target": 90,
                  "grossTarget": 90
                },
                {
                  "id": "B7D91DEF-1693-42E5-9915-29BA1FBDC7F1",
                  "name": "37 - 54",
                  "attributes": [

                  ],
                  "target": 90,
                  "grossTarget": 90
                },
                {
                  "id": "64400EBC-D173-4B8F-B5B4-F1B335BE78B9",
                  "name": "55 or above",
                  "attributes": [

                  ],
                  "target": 90,
                  "grossTarget": 90
                }
              ]
            }
          ],
          "target": 360,
          "grossTarget": 360
        },
        {
          "id": "D415B9F7-13A6-4954-965E-8BE65BE41BE9",
          "name": "Barcelona",
          "attributes": [
            {
              "id": "4F83155C-9FC5-4025-9E9C-5758600246AC",
              "name": "Gender",
              "odinVariable": "qGender",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "9D41635C-9526-407E-ABCB-44249530D3FA",
                  "name": "Male",
                  "attributes": [

                  ],
                  "target": 200,
                  "grossTarget": 200
                },
                {
                  "id": "777E289C-1D17-49CB-9E6C-23269D52732B",
                  "name": "Female",
                  "attributes": [

                  ],
                  "target": 120,
                  "grossTarget": 120
                }
              ]
            },
            {
              "id": "33C24DD2-BF84-49E5-8AE2-C4A6CE570F99",
              "name": "Age",
              "odinVariable": "qAge",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "FF1A2758-5F26-4AF0-9C62-F31742540EFC",
                  "name": "0 - 18",
                  "attributes": [

                  ],
                  "target": 80,
                  "grossTarget": 80
                },
                {
                  "id": "AE20EC15-A370-4C3D-AAAB-453FDE9723E3",
                  "name": "19 - 36",
                  "attributes": [

                  ],
                  "target": 80,
                  "grossTarget": 80
                },
                {
                  "id": "0FEE8B9B-3469-411A-8C67-37C387B1C80F",
                  "name": "37 - 54",
                  "attributes": [

                  ],
                  "target": 80,
                  "grossTarget": 80
                },
                {
                  "id": "74C92A44-7AC7-4934-820D-8BFA55B551BF",
                  "name": "55 or above",
                  "attributes": [

                  ],
                  "target": 80,
                  "grossTarget": 80
                }
              ]
            }
          ],
          "target": 320,
          "grossTarget": 320
        },
        {
          "id": "F14FE62F-075D-4BB6-9E2A-A62B7ABA2946",
          "name": "Chicago",
          "attributes": [
            {
              "id": "2883A667-29AD-4790-8B37-AFA003145D65",
              "name": "Gender",
              "odinVariable": "qGender",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "20877B5C-425F-41B0-9FD1-0B05994F17B0",
                  "name": "Male",
                  "attributes": [

                  ],
                  "target": 140,
                  "grossTarget": 140
                },
                {
                  "id": "6DF66BAE-7C96-45BB-A70F-EED7423C64A0",
                  "name": "Female",
                  "attributes": [

                  ],
                  "target": 140,
                  "grossTarget": 140
                }
              ]
            },
            {
              "id": "1809D22B-84BC-4433-80E1-5D02C5C709BF",
              "name": "Age",
              "odinVariable": "qAge",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "8121D931-1331-4BA6-8566-6AB66E9B6C01",
                  "name": "0 - 18",
                  "attributes": [

                  ],
                  "target": 70,
                  "grossTarget": 70
                },
                {
                  "id": "AF76824D-141E-49FE-9D65-390143FE0E86",
                  "name": "19 - 36",
                  "attributes": [

                  ],
                  "target": 70,
                  "grossTarget": 70
                },
                {
                  "id": "21692522-923E-4294-8598-650FAFC015CE",
                  "name": "37 - 54",
                  "attributes": [

                  ],
                  "target": 70,
                  "grossTarget": 70
                },
                {
                  "id": "72F                E624F-467A-4E79-8827-B8F78DF16574",
                  "name": "55 or above",
                  "attributes": [

                  ],
                  "target": 70,
                  "grossTarget": 70
                }
              ]
            }
          ],
          "target": 280,
          "grossTarget": 280
        },
        {
          "id": "C7CB6BC4-E43F-4F3E-8066-4A6A18D52FAE",
          "name": "Dallas",
          "attributes": [
            {
              "id": "4EA927B9-32BA-4B28-9506-4416244AD3AD",
              "name": "Gender",
              "odinVariable": "qGender",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "18405B4A-951F-4F89-AFFB-E6971092FE50",
                  "name": "Male",
                  "attributes": [

                  ],
                  "target": 150,
                  "grossTarget": 150
                },
                {
                  "id": "477E6B8E-0776-4A40-A487-A1DE6A8C4D9E",
                  "name": "Female",
                  "attributes": [

                  ],
                  "target": 90,
                  "grossTarget": 90
                }
              ]
            },
            {
              "id": "39410E36-7F32-4ADC-A038-1458E006B5F9",
              "name": "Age",
              "odinVariable": "qAge",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "3AF8109A-3547-4DF8-B251-B74039A668B4",
                  "name": "0 - 18",
                  "attributes": [

                  ],
                  "target": 60,
                  "grossTarget": 60
                },
                {
                  "id": "866001DD-1126-4022-84C1-A8D9B0DA20B8",
                  "name": "19 - 36",
                  "attributes": [

                  ],
                  "target": 60,
                  "grossTarget": 60
                },
                {
                  "id": "A2AFA75D-2B49-4658-8F6A-E517770CE354",
                  "name": "37 - 54",
                  "attributes": [

                  ],
                  "target": 60,
                  "grossTarget": 60
                },
                {
                  "id": "70B9AB14-5630-4644-                BA00-7740FCD8C343",
                  "name": "55 or above",
                  "attributes": [

                  ],
                  "target": 60,
                  "grossTarget": 60
                }
              ]
            }
          ],
          "target": 240,
          "grossTarget": 240
        },
        {
          "id": "F60443C1-D28D-46A4-803C-8F16024CFAC7",
          "name": "Eastbourne",
          "attributes": [
            {
              "id": "947769C6-D35C-42D3-92B4-5FF992D817E8",
              "name": "Gender",
              "odinVariable": "qGender",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "2AF91D23-4795-4941-AA60-93FEA15FB072",
                  "name": "Male",
                  "attributes": [

                  ],
                  "target": 100,
                  "grossTarget": 100
                },
                {
                  "id": "2071BE05-F1EE-4FF6-AD8D-A67BB5268087",
                  "name": "Female",
                  "attributes": [

                  ],
                  "target": 100,
                  "grossTarget": 100
                }
              ]
            },
            {
              "id": "F554F486-97DF-472F-A64C-9D2824A7764D",
              "name": "Age",
              "odinVariable": "qAge",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "254B349B-EB21-4AA3-B3CB-23EC03EAC68B",
                  "name": "0 - 18",
                  "attributes": [

                  ],
                  "target": 50,
                  "grossTarget": 50
                },
                {
                  "id": "502B2284-F2FE-4AC4-88BC-F1FD366E3853",
                  "name": "19 - 36",
                  "attributes": [

                  ],
                  "target": 50,
                  "grossTarget": 50
                },
                {
                  "id": "435C968C-AFF9-410E-BF50-F2EB03BE016C",
                  "name": "37 - 54",
                  "attributes": [

                  ],
                  "target": 50,
                  "grossTarget": 50
                },
                {
                  "id": "E1E0DD25-3F6B-45E4-8FD8-F22B6              3E416D2",
                  "name": "55 or above",
                  "attributes": [

                  ],
                  "target": 50,
                  "grossTarget": 50
                }
              ]
            }
          ],
          "target": 200,
          "grossTarget": 200
        },
        {
          "id": "69922F85-8F65-4C60-9F2B-04DA2E5C5E2F",
          "name": "Florence",
          "attributes": [
            {
              "id": "B161226B-5156-4E27-BE31-DECB3A68B65C",
              "name": "Gender",
              "odinVariable": "qGender",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "6BCD665C-4A58-49C8-A904-38655F15563F",
                  "name": "Male",
                  "attributes": [

                  ],
                  "target": 100,
                  "grossTarget": 100
                },
                {
                  "id": "5A63EB76-242A-40C6-9E92-41E29AFD131F",
                  "name": "Female",
                  "attributes": [

                  ],
                  "target": 60,
                  "grossTarget": 60
                }
              ]
            },
            {
              "id": "3EEA5B4C-CE49-4278-8BE7-4F71DBF6BF46",
              "name": "Age",
              "odinVariable": "qAge",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "4E21FD28-16FF-48C8-BD0F-6E867E7D48D5",
                  "name": "0 - 18",
                  "attributes": [

                  ],
                  "target": 40,
                  "grossTarget": 40
                },
                {
                  "id": "D6B3523C-3DBF-4F3B-94A6-25F89EB2A099",
                  "name": "19 - 36",
                  "attributes": [

                  ],
                  "target": 40,
                  "grossTarget": 40
                },
                {
                  "id": "6A266E81-922B-4E1E-AC25-75A7076F9B6F",
                  "name": "37 - 54",
                  "attributes": [

                  ],
                  "target": 40,
                  "grossTarget": 40
                },
                {
                  "id": "E15860C4-A641-4397-AD99-6FA8B6D007CB",
                  "name                 ": "55 or above",
                  "attributes": [

                  ],
                  "target": 40,
                  "grossTarget": 40
                }
              ]
            }
          ],
          "target": 160,
          "grossTarget": 160
        },
        {
          "id": "31B2E5CD-6E19-40DC-9868-F559FD0077EF",
          "name": "Geneva",
          "attributes": [
            {
              "id": "00503228-A2B7-4D4D-ACE9-D027176C5D35",
              "name": "Gender",
              "odinVariable": "qGender",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "137D1CF8-31F7-4776-859A-EB95690E709A",
                  "name": "Male",
                  "attributes": [

                  ],
                  "target": 60,
                  "grossTarget": 60
                },
                {
                  "id": "AEACCBE1-AEEE-4340-85DA-683AA4CFB116",
                  "name": "Female",
                  "attributes": [

                  ],
                  "target": 60,
                  "grossTarget": 60
                }
              ]
            },
            {
              "id": "4C9B3515-ADCA-4450-82D9-E8021FF0701B",
              "name": "Age",
              "odinVariable": "qAge",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "20352BE8-FA7D-4E6E-8B2D-1C06517D3823",
                  "name": "0 - 18",
                  "attributes": [

                  ],
                  "target": 30,
                  "grossTarget": 30
                },
                {
                  "id": "36CC8AAF-F047-4F65-8F38-9BF9B9E6AE76",
                  "name": "19 - 36",
                  "attributes": [

                  ],
                  "target": 30,
                  "grossTarget": 30
                },
                {
                  "id": "05FA89A6-2DD5-4349-A814-0F9455A3E32C",
                  "name": "37 - 54",
                  "attributes": [

                  ],
                  "target": 30,
                  "grossTarget": 30
                },
                {
                  "id": "1DC83450-FE87-47BF-A6D6-7B3CD95201AC",
                  "name": "55 or above",
                  "a                ttributes": [

                  ],
                  "target": 30,
                  "grossTarget": 30
                }
              ]
            }
          ],
          "target": 120,
          "grossTarget": 120
        },
        {
          "id": "F3DC3470-6069-410E-AC48-7C725772674E",
          "name": "Hong Kong",
          "attributes": [
            {
              "id": "63B10C4F-E776-4CB8-A831-50B96F307F28",
              "name": "Gender",
              "odinVariable": "qGender",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "5C9C9D2D-5E5B-4E93-9749-4F675B1C7F38",
                  "name": "Male",
                  "attributes": [

                  ],
                  "target": 30,
                  "grossTarget": 30
                },
                {
                  "id": "B717F561-CAB9-408F-8C94-6F877A1BE119",
                  "name": "Female",
                  "attributes": [

                  ],
                  "target": 50,
                  "grossTarget": 50
                }
              ]
            },
            {
              "id": "97D73FF7-2F44-4FC7-9733-478ABBA4F107",
              "name": "Age",
              "odinVariable": "qAge",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "5AC03E4F-C858-4119-AA1E-EC67E7E6A07F",
                  "name": "0 - 18",
                  "attributes": [

                  ],
                  "target": 20,
                  "grossTarget": 20
                },
                {
                  "id": "3B68BFF5-1B3C-4CB7-B1DA-C34D973887E3",
                  "name": "19 - 36",
                  "attributes": [

                  ],
                  "target": 20,
                  "grossTarget": 20
                },
                {
                  "id": "D60452EA-30C4-4ABD-91EB-0678AA01F905",
                  "name": "37 - 54",
                  "attributes": [

                  ],
                  "target": 20,
                  "grossTarget": 20
                },
                {
                  "id": "14F833DD-43AA-4C8F-B641-DFDE4B64166A",
                  "name": "55 or above",
                  "attributes": [

                  ],
                  "                 target": 20,
                  "grossTarget": 20
                }
              ]
            }
          ],
          "target": 80,
          "grossTarget": 80
        },
        {
          "id": "57B0A054-F280-4FEE-8D35-3F7F745D3972",
          "name": "Istanbul",
          "attributes": [
            {
              "id": "6A1E7FC4-0269-447E-AE8B-E1A0ABE8928A",
              "name": "Gender",
              "odinVariable": "qGender",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "6F51E322-D62E-4A7D-AB2E-F40045D012E5",
                  "name": "Male",
                  "attributes": [

                  ],
                  "target": 20,
                  "grossTarget": 20
                },
                {
                  "id": "FCE3CB5A-90DB-4C34-AD6A-02E44E7A2E88",
                  "name": "Female",
                  "attributes": [

                  ],
                  "target": 20,
                  "grossTarget": 20
                }
              ]
            },
            {
              "id": "664740F7-8536-4138-8FDD-7F6426EEACDE",
              "name": "Age",
              "odinVariable": "qAge",
              "isSelectionOptional": "False",
              "levels": [
                {
                  "id": "10F00D6D-7826-4F8C-8994-BEC39F549EA1",
                  "name": "0 - 18",
                  "attributes": [

                  ],
                  "target": 10,
                  "grossTarget": 10
                },
                {
                  "id": "4A93CE11-D338-4D84-B585-EC6FD62D0607",
                  "name": "19 - 36",
                  "attributes": [

                  ],
                  "target": 10,
                  "grossTarget": 10
                },
                {
                  "id": "4964CDD5-201A-444B-B0D7-ABFE7D231042",
                  "name": "37 - 54",
                  "attributes": [

                  ],
                  "target": 10,
                  "grossTarget": 10
                },
                {
                  "id": "77D173CE-3ECC-40C9-BF9B-01FBC0A6E5AD",
                  "name": "55 or above",
                  "attributes": [

                  ],
                  "target": 10,
                  "grossT               arget": 10
                }
              ]
            }
          ],
          "target": 40,
          "grossTarget": 40
        }
      ]
    }
  ],
  "target": 960,
  "grossTarget": 960
}

以下是我在 C# 中尝试过的内容:

public class Level
{
    public string id { get; set; }
    public string name { get; set; }
    public int target { get; set; }
    public int grossTarget { get; set; }
    public List<object> attributes { get; set; }
}

public void Read(DataTable dtQuotaFrame)
{
    List<Level> LevelList = new List<Level>();
    int LastLevelRowNumber = 0;
    for (int RowIndex = StartRowIndex; RowIndex < dtQuotaFrame.Rows.Count; RowIndex++)
    {
        for (int ColumnIndex = 1; ColumnIndex < dtQuotaFrame.Columns.Count; ColumnIndex++)
        {
            string CellValue = dtQuotaFrame.Rows[RowIndex][ColumnIndex].ToString().Trim();

            int n;
            bool isNumeric = int.TryParse(CellValue, out n);

            if (isNumeric)
            {
                if (LastLevelRowNumber == 0)
                {
                    LastLevelRowNumber = RowIndex - 1;
                }

                string LastLevelCellValue = dtQuotaFrame.Rows[LastLevelRowNumber][ColumnIndex].ToString().Trim();

                if (LastLevelCellValue == "Target")
                {

                }
                else
                {
                    string[] CellValues = LastLevelCellValue.ToString().Trim().Split('/');
                    string Level = CellValues[0];
                    string Type = CellValues[1];
                    string Name = CellValues[2];
                    string Variable = CellValues[3];
                    string Mandatory = CellValues[4];
                    string TableNo = CellValues[5];

                    Level level = new Level();
                    level.id = System.Guid.NewGuid().ToString();
                    level.name = Name;
                    level.target = n;
                    level.grossTarget = n;

                    LevelList.Add(level);
                }
            }
        }
    }
}

任何帮助将不胜感激。谢谢你。

标签: c#jsondictionaryobject

解决方案


您可以dynamic用于此目的。

dynamic obj = JsonConvert.DeserializeObject(File.ReadAllText("json.json"));

推荐阅读