简体   繁体   English

如何从.net核心中的json文件读取复杂的对象

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

I have a json like this to read in c# but it showing null. 我有一个这样的json可以在c#中读取,但显示为null。

Provices.json contains this json 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
          }
    ]
  }
}

start up file contains 启动文件包含

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

This is c# models 这是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; }

    }

This is how I am reading this file which is giving me null values 这就是我读取此文件的方式,该文件为我提供了空值

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>(); 
        }

}

I am also reading AppSettings.json which is working fine but province.json is not working.Can You please tell what's wrong i did 我也正在阅读AppSettings.json,它可以正常工作,但province.json无法正常工作。请问我做错了什么

You have both Published and DisplayOrder declared as bool but in the file, the values are: 您已将PublishedDisplayOrder声明为bool但在文件中,值是:

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

Also, when you read your values into data in the code below, you return a new empty list. 同样,当您在下面的代码中将值读入data时,将返回一个新的空列表。 You need to return data instead. 您需要返回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();

If you call as separate file like this then it will come, or else you can add the contents of Provinces to your appconfig json file and get it as section for that your current code will work. 如果您这样调用作为单独的文件,则它将来,否则您可以将Provinces的内容添加到您的appconfig json文件中,并将其作为该节的一部分,以使您的当前代码可用。

I got my problem. 我有我的问题。

Well, the answer was in type. 好吧,答案是正确的。

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

On commenting out these lines, it started working. 在注释掉这些行后,它开始工作。

Second Mistake is to add json file in Program.cs[.net core version 2.0] 第二个错误是在Program.cs [.net核心版本2.0]中添加json文件

What is observe, some of fields are missing to make them as nullable like DisplayOrder eg json is not loading because of null property. 可以看到,缺少某些字段使它们像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; }

    }

Note: Expect string property no need make them as null, rest type of parameter we need to make him as null and if you sure property also contain value then no need made him as a null parameter. 注意:期望字符串属性不需要将其设置为null,其余类型的参数需要将其设置为null,如果您确定属性也包含值,则无需将其设置为null参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 JSON 文件反序列化 .NET Core 中封装的对象表? - How to deserialize encapsulated table of objects in .NET Core from JSON file? 如何从 ASP.NET Core 中的 .json 文件中读取 AppSettings 值 - How to read AppSettings values from a .json file in ASP.NET Core 如何使用 Razor 页面从 ASP.net 核心简单 web 应用程序中的配置读取 json 对象数组 - How to read array of json objects from configuration in ASP.net Core simple web app using Razor pages 如何从.Net Core 中的多个 json 文件中读取值? - How to read values from multiple json files in .Net Core? 如何从 .NET 核心中的 appsettings.json 读取部分值 - How to read section values from appsettings.json in .NET Core .Net Core-IConfigurationRoot读取整个json文件 - .Net Core - IConfigurationRoot read entire json file .NET Core - 转换读取JSON文件的日期 - .NET Core - converting date of read JSON file 从视图向控制器传递.Net Core中的多个复杂对象 - Passing Multiple Complex Objects in .Net Core From View To Controller 如何避免多个查询EF .net核心中的一个复杂对象 - How to avoid multiple queries one complex objects in EF .net core 如何使用.NET Core从本地SQLite文件读取? - How do you read from a local SQLite file with .NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM