如何在.Net Core中讀取Json配置文件?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創新互聯是專業的眉縣網站建設公司,眉縣接單;提供網站設計制作、成都做網站,網頁設計,網站設計,建網站,PHP網站建設等專業做網站服務;采用PHP框架,可快速的進行眉縣網站開發網頁制作和功能擴展;專業做搜索引擎喜愛的網站,專業的做網站團隊,希望更多企業前來合作!第一種:使用IConfiguration接口
我們先在appsettings.json中配置好數據庫連接字符串,然后讀取它
{
"Connection": {
"dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
在控制器中注入IConfiguration接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
namespace Read.json.Controllers
{
[ApiController]
[Route("[controller]")]
public class ReadController : Controller
{
private IConfiguration _configuration;
public ReadController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpPost]
public async Task<string> ReadJson()
{
//讀參
string conn = _configuration["Connection:dbContent"];
return "";
}
}
}
當然也可以讀取數組形式的json,一樣的先在appsettings.json中寫好配置參數,如下:
{
"Connection": {
"dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456"
},
//------------------------
"Content": [
{
"Trade_name": {
"test1": "小熊餅干",
"test2": "旺仔QQ糖",
"test3": "娃哈哈牛奶"
}
}
],
//------------------------
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}比如我們想讀取test1
string commodity_test1 = _configuration["Content:0:Trade_name:test1"];
第二種:使用IOptions<T>來讀取json配置文件
先把NuGet包導進項目:Microsoft.Extensions.Options.ConfigurationExtensions

首先在appsettings.json中添加節點如下
{
"Connection": {
"dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456"
},
//------------------------
"Content": [
{
"Trade_name": {
"test1": "小熊餅干",
"test2": "旺仔QQ糖",
"test3": "娃哈哈牛奶"
}
}
],
//------------------------
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
//==============================
"Information": {
"school": {
"Introduce": {
"Name": "實驗小學",
"Class": "中班",
"Number": "15人"
},
"Region": {
"Province": "湖北",
"City": "武漢",
"Area": "洪山區"
},
"Detailed_address": [
{
"Address": "佳園路207號"
}
]
}
}
//==============================
}然和再建立一個與這個節點"相同"的類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Read.json
{
public class Information
{
public School school { get; set; }
}
public class School
{
public Introduce Introduce { get; set; }
public Region Region { get; set; }
public List<Detailed_address> data { get; set; }
}
public class Introduce
{
public string Name { get; set; }
public string Class { get; set; }
public string Number { get; set; }
}
public class Region
{
public string Province { get; set; }
public string City { get; set; }
public string Area { get; set; }
}
public class Detailed_address
{
public string Address { get; set; }
}
}在Startup中添加如下代碼
#region 服務注冊,在控制器中通過注入的形式使用
services.AddOptions();
services.Configure<Information>(Configuration.GetSection("Information"));
#endregion
控制器中使用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
namespace Read.json.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ReadController : Controller
{
private IConfiguration _configuration;
readonly Information _Information;
readonly IOptions<Information> _options;
public ReadController(IConfiguration configuration,
Information Information,
IOptions<Information> options)
{
_configuration = configuration;
_Information = Information;
_options = options;
}
[HttpGet]
public async Task<IActionResult> ReadInformation()
{
string Address = _options.Value.school.Region.Province + "-" +
_options.Value.school.Region.City + "-" +
_options.Value.school.Region.Area + "-" +
_options.Value.school.Detailed_address[0].Address + "-" +
_options.Value.school.Introduce.Name + "-" +
_options.Value.school.Introduce.Class + "-" +
_options.Value.school.Introduce.Number;
return Json(Address);
}
[HttpPost]
public async Task<string> ReadJson()
{
string conn = _configuration["Connection:dbContent"];
string commodity = _configuration["Content:0:Trade_name:test1"];
return "";
}
}
}

第三種:這種應該比較常見,任意讀取自定義的json文件
首先建立一個json文件
{
"system_version": {
"Edition": ".Net Core 3.0",
"Project_Name": "Read.json"
}
}
再建一個類,封裝一個方法
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Read.json
{
public class Json_File
{
public IConfigurationRoot Read_Json_File()
{
//這句代碼會讀取read_json.json中的內容
return new ConfigurationBuilder().AddJsonFile("read_json.json")
.Build();
}
}
}
在控制器中調用:
[HttpGet]
public async Task<IActionResult> ReadSystemVersion()
{
var configuration = _json_File.Read_Json_File();
string system = "使用的是" + configuration["system_version:Edition"] + "的版本" + "," +
"項目名稱是" + configuration["system_version:Project_Name"];
return Json(new
{
data = system
});
}

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創新互聯行業資訊頻道,感謝您對創新互聯網站建設公司,的支持。
新聞標題:如何在.NetCore中讀取Json配置文件-創新互聯
網頁地址:http://www.yijiale78.com/article2/cdpsic.html
成都網站建設公司_創新互聯,為您提供Google、品牌網站設計、企業網站制作、營銷型網站建設、關鍵詞優化、外貿網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯