首页 > 解决方案 > 如何绑定这个json并读取特定的键值

问题描述

我有下面的 json,我想在列表中阅读它。下面是定义的类和模型以及我试图阅读的代码。我在绑定时得到空值。我不知道我应该如何实现这一点。因此,例如,如果我有多个规则,我想根据通过的条件阅读每个规则。请参阅我的上一个示例代码以更好地理解。

示例 Json:

{"TableStorageRule": { "Rules": [ {
    "Name": "filterRule1",
    "DataFilter":
    {
      "DataSetType": "Settings1"

    },
    "TableSettings":
    {
      "AzureTable": {
        "Account": "account1",
        "Table": "table1",
        "Key": "key1"
      },
      "SchemaBaseUri": "https://test.web.core.windows.net/"
    }

  },
  {
    "Name": "filterRule2",
    "DataFilter":
    {
      "DataSetType": "Settings2"

    },
    "TableSettings":
    {
      "AzureTable": {
        "Account": "account2",
        "Table": "table2",
        "Key": "key2"
      },
      "SchemaBaseUri": "https://test2.web.core.windows.net/"
    }

  }
  
  
]  }}

型号和代码:

public class TableStoreSettings
{
    public class AzureTableSettings
    {
        public string Account { get; set; }
        public string Key { get; set; }
        public string Table { get; set; }
    }

    public AzureTableSettings AzureTable { get; set; }

    public Uri SchemaBaseUri { get; set; }
}

public class TableStorageRule
{
    public string Name { get; set; }
    public TwisterDataFilter DataFilter { get; set; }
    public TableStoreSettings TableSettings { get; set; }
}

public class TableStorageConfiguration
{
    public IEnumerable<TableStorageRule> Rules { get; set; } = new List<TableStorageRule>();
}

我试图阅读的代码:

 var builder = new ConfigurationBuilder()
        .SetBasePath(Path.Combine(Root))
        .AddJsonFile("appsettings.json", optional: false);

        var config = builder.Build();
        
        var tableStorageOutput = new TableStorageRule();            
        config.GetSection("TableStorageRule").Bind(tableStorageOutput);
        var nameOfFilter = tableStorageOutput.Name;

        if (tableStorageOutput.Name == "filterRule1")
        {
            var accountname = tableStorageOutput.TableSettings.AzureTable.Account;
        }

在上面我只得到第一个 filtername1 ,我没有得到其他 filternames 等等......尽管在 GetSection() 上我看到了快速观看的所有值。

标签: c#asp.net-core

解决方案


这是您的 JSON 文件中的正确模型:

public class DataFilter    {
    public string DataSetType { get; set; } 
}

public class AzureTable    {
    public string Account { get; set; } 
    public string Table { get; set; } 
    public string Key { get; set; } 
}

public class TableSettings    {
    public AzureTable AzureTable { get; set; } 
    public string SchemaBaseUri { get; set; } 
}

public class Rule    {
    public string Name { get; set; } 
    public DataFilter DataFilter { get; set; } 
    public TableSettings TableSettings { get; set; } 
}

public class TableStorageRule    {
    public List<Rule> Rules { get; set; } 
}

public class Root    {
    public TableStorageRule TableStorageRule { get; set; } 
}

对于测试,您可以读取您的 JSON 文件并获取值(也许更改模型可以解决您的代码中的问题):

string json = File.ReadAllText(jsonFilePath);
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(json);

推荐阅读