簡體   English   中英

如何從.net核心中的json文件讀取復雜的對象

[英]How to read complex objects from json file in .net core

我有一個這樣的json可以在c#中讀取,但顯示為null。

Provices.json包含此json

{
   "Provinces": {
        "States": [
          {
            "CountryId": 1,
            "Name": "AA (Armed Forces Americas)",
            "Abbreviation": null,
            "Published": false,
            "DisplayOrder": 0,
            "Country": null,
            "Id": 1
          },
          {
            "CountryId": 1,
            "Name": "AE (Armed Forces Europe)",
            "Abbreviation": null,
            "Published": false,
            "DisplayOrder": 0,
            "Country": null,
            "Id": 54
          }
    ]
  }
}

啟動文件包含

 services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
 services.Configure<Provinces>(Configuration.GetSection("Provinces"));

這是C#模型

   public class Provinces
    {
        public Provinces()
        {
            this.States = new List<State>();
        }
        public List<State> States { get; set; }
    }

    public class State
    {
        public int CountryId { get; set; }

        public string Name { get; set; }

        public string Abbreviation { get; set; }

        public bool Published { get; set; }

        public bool DisplayOrder { get; set; }

        public string Country { get; set; }

        public int Id { get; set; }

    }

這就是我讀取此文件的方式,該文件為我提供了空值

public class UserService
{
 public UserService(IOptions<AppSettings> appSettings, 
  IOptions<Provinces> states)
    {
           _appSettings = appSettings;
            _states = states;
     }

//Getting value from json


 public List<State> GetStates()
        {
            var data = this._states.Value.States; // Zero count shows here
            return new List<State>(); 
        }

}

我也正在閱讀AppSettings.json,它可以正常工作,但province.json無法正常工作。請問我做錯了什么

您已將PublishedDisplayOrder聲明為bool但在文件中,值是:

"Published": false,
"DisplayOrder": 0,

同樣,當您在下面的代碼中將值讀入data時,將返回一個新的空列表。 您需要返回data

public List<State> GetStates()
    {
        var data = this._states.Value.States; // Zero count shows here
        return new List<State>(); 
    }
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJson(yourconfigSetting.json optional: true, reloadOnChange: true)
.AddJsonFile("Provinces.json", optional: true, reloadOnChange: true)
.Build();

如果您這樣調用作為單獨的文件,則它將來,否則您可以將Provinces的內容添加到您的appconfig json文件中,並將其作為該節的一部分,以使您的當前代碼可用。

我有我的問題。

好吧,答案是正確的。

        "CountryId": 1,
        "Name": "AE (Armed Forces Europe)",
       // "Abbreviation": null,
       // "Published": false,
       //"DisplayOrder": 0,
       // "Country": null,
        "Id": 54

在注釋掉這些行后,它開始工作。

第二個錯誤是在Program.cs [.net核心版本2.0]中添加json文件

可以看到,缺少某些字段使它們像DisplayOrder一樣可為空,例如,由於null屬性而未加載json。

public class State
    {
        public int CountryId { get; set; }

        public string Name { get; set; }

        public string Abbreviation { get; set; }

        public bool Published { get; set; }

        public bool DisplayOrder { get; set; }

        public string Country { get; set; }

        public int Id { get; set; }

    }

注意:期望字符串屬性不需要將其設置為null,其余類型的參數需要將其設置為null,如果您確定屬性也包含值,則無需將其設置為null參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM