Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用json.net和多个列表反序列化json对象_C#_Json.net - Fatal编程技术网

C# 使用json.net和多个列表反序列化json对象

C# 使用json.net和多个列表反序列化json对象,c#,json.net,C#,Json.net,我有以下模型,我想从class属性访问名称和类型: public partial class RootObject { [JsonProperty("edmx:Edmx")] public EdmxEdmx EdmxEdmx { get; set; } } public partial class EdmxEdmx { [JsonProperty("EntityType")] public Lis

我有以下模型,我想从class属性访问名称和类型:

 public partial class RootObject
    {
        [JsonProperty("edmx:Edmx")]
        public EdmxEdmx EdmxEdmx { get; set; }
    }

    public partial class EdmxEdmx
    {
        [JsonProperty("EntityType")]
        public List<EntityType> EntityType { get; set; }
    }

    public partial class EntityType
    {
        [JsonProperty("-Name")]
        public string Name { get; set; }

        [JsonProperty("Property")]
        public List<Property> Property { get; set; }
    }

    public partial class Property
    {
        [JsonProperty("-Name")]
        public string Name { get; set; }

        [JsonProperty("-Type")]
        public String Type { get; set; }

    }
上面的JSON是o1.ToString。我使用JObject从文本文件创建JSON对象

编辑2:

让我试着更准确地说: 我想按每个实体获取此信息:

我不确定是什么让你在这件事上心烦意乱。你拥有了所有的片段,你所需要的只是一个嵌套的foreach循环

foreach (var entityType in r.EdmxEdmx.EntityType)
{
    foreach (var property in entityType.Property)
    {
        Console.WriteLine("-Name: " + property.Name);
        Console.WriteLine("-Type: " + property.Type);
        Console.WriteLine();
    }
}

小提琴手:

对不起,jsonOkay补充道,所以你有那么多。。。你被困在哪里还不清楚。请注意,每个实体类型可以有多个实体类型和多个属性。。。你想用这些做什么,现在发生了什么?它没有失败。我只想访问class属性中的Name和Type。我可以执行以下操作:字符串Name=r.EdmxEdmx.EntityType[0]。Name。这给了我实体的名称。然后我需要访问该实体的名称和类型。现在还不清楚你的意思。唯一的
类型
属性在
属性
中,并且有多个属性。您希望从显示的JSON中获得什么值?我的问题是,我不知道我必须使用嵌套循环。谢谢你,布莱恩。
{
  "edmx:Edmx": {
    "-xmlns:edmx": "http://docs.oasis-open.org/odata/ns/edmx",
    "-Version": "4.0",
    "EntityType": [
      {
        "-BaseType": "mscrm.crmbaseentity",
        "-Name": "EntityA",
        "Property": [
          {
            "-Name": "address2_line1",
            "-Unicode": "false",
            "-Type": "Edm.String"
          },
          {
            "-Name": "territorycode",
            "-Type": "Edm.Int32"
          },
          {
            "-Name": "EntityID",
            "-Type": "Edm.Guid"
          },
          {
            "-Name": "address1_telephone1",
            "-Unicode": "false",
            "-Type": "Edm.String"
          }
        ]
      }
}
}
foreach (var entityType in r.EdmxEdmx.EntityType)
{
    foreach (var property in entityType.Property)
    {
        Console.WriteLine("-Name: " + property.Name);
        Console.WriteLine("-Type: " + property.Type);
        Console.WriteLine();
    }
}