C#动态类型Object属性First始终为null

时间:2019-07-18 10:33:30

标签: c# json dynamic json.net enumerable

我正在使用Newtonsoft.Json在C#中创建一个动态对象,并尝试对其成员进行迭代。

Json布局等于:

[{{"id": "Name"}, {"spec": {More Data}}, {{"id": "Name"}, {"spec": {More Data}}, ...]

当我尝试以下操作时:

dynamic jsonObj = JsonConvert.DeserializeObject(jsonString);

      foreach (dynamic entry in jsonObj){

            dynamic first = entry.First;
      }

我首先收到一个对象,该对象包含null,即使该条目具有运行时生成的成员First和Last,它们也不是null(当我在Visual Studio中以调试方式查看它们时)。

可以访问成员。使用时:

dynamic first = entry["id"];

我收到正确的值,而不是null。因此内部必须有数据。

是否不可能在循环中访问运行时生成的成员,还是有另一个错误?

PS:我知道,我必须调用entry.First.First才能访问"id"的值。但这也只返回null

动态对象内容的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

using Newtonsoft.Json;
using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string jsonString = @"
[
    [   {   item1: { 'id':      'Name'      },
            item2: { 'spec':    'More Data' }
        },
        {   item1: { 'id':      'Name'      },
            item2: { 'spec':    'More Data' }
        }
    ]
]";

            dynamic jsonObj = JsonConvert.DeserializeObject(jsonString);

            foreach (dynamic row in jsonObj)
            {
                Console.WriteLine($"Row\n{row}\n--------------------");
                foreach (var column in row)
                {
                    Console.WriteLine($"Column\n{column}\n--------------------");
                    foreach (var item in column)
                    {
                        Console.WriteLine($"item\n{item}\n--------------------");
                        foreach (var part in item)
                        {
                            Console.WriteLine($"part\n{part}\n--------------------");
                            foreach (var subpart in part)
                            {
                                Console.WriteLine($"subpart\n{subpart}\n--------------------");
                                foreach (var subsubpart in subpart)
                                {
                                    Console.WriteLine($"subsubpart\n{subsubpart}\n--------------------");
                                }
                            }
                        }
                    }
                }
            }

            Console.ReadKey();
        }
    }
}

礼物:

Row
[
  {
    "item1": {
      "id": "Name"
    },
    "item2": {
      "spec": "More Data"
    }
  },
  {
    "item1": {
      "id": "Name"
    },
    "item2": {
      "spec": "More Data"
    }
  }
]
--------------------
Column
{
  "item1": {
    "id": "Name"
  },
  "item2": {
    "spec": "More Data"
  }
}
--------------------
item
"item1": {
  "id": "Name"
}
--------------------
part
{
  "id": "Name"
}
--------------------
subpart
"id": "Name"
--------------------
subsubpart
Name
--------------------
item
"item2": {
  "spec": "More Data"
}
--------------------
part
{
  "spec": "More Data"
}
--------------------
subpart
"spec": "More Data"
--------------------
subsubpart
More Data
--------------------
Column
{
  "item1": {
    "id": "Name"
  },
  "item2": {
    "spec": "More Data"
  }
}
--------------------
item
"item1": {
  "id": "Name"
}
--------------------
part
{
  "id": "Name"
}
--------------------
subpart
"id": "Name"
--------------------
subsubpart
Name
--------------------
item
"item2": {
  "spec": "More Data"
}
--------------------
part
{
  "spec": "More Data"
}
--------------------
subpart
"spec": "More Data"
--------------------
subsubpart
More Data
--------------------