[英]How to read sub array of values from appsetting.json using IConfiguration?
我需要的是通過將名稱值與數據庫查詢中的值相匹配來獲取 GroupAssets SearchPath 值。 在這一點上,我只是試圖將 GroupAssets 拉入一個數組。
應用程序設置.json
"Production": {
"PrintJobs": [
{
"SalesCategory": "5084",
"JobType": 1,
"SubJobType": 5014,
"ShipVia": 5019,
"CSR": 360,
"SLAHours": 216,
"DaysToArrive": 5,
"Note": [ "" ],
"GroupAssets": [
{
"Name": "MapImage",
"SearchPath": "\\\\Server\\Path",
"PrintAssetType": "Image",
"ValidExtensions": [ "jpg" ]
},
{
"Name": "PageImage",
"SearchPath": "\\\\Server\\Path",
"PrintAssetType": "Image",
"ValidExtensions": [ "jpg" ],
"CreativeCodes": [ "M1YV", "M1YW" ]
},
{
"Name": "ItineraryPage",
"SearchPath": "\\\\Server\\Path",
"PrintAssetType": "Pdf",
"ValidExtensions": [ "pdf" ],
"CreativeCodes": [ "M1YV", "M1YW" ]
}
],
}
}
我的代碼:
var myArray = _config.GetSection("Production:PrintJobs").GetChildren();
public class PrintAssetDefns
{
public string Name { get; set; }
public string SearchPath { get; set; }
public string PrintAssetType { get; set; }
public string ValidExtensions { get; set; }
public string CreativeCodes { get; set; }
}
嘗試這個
PrintAssetDefns[] MyArray = configuration.GetSection("Production:PrintJobs")
.GetChildren().First().GetSection("GroupAssets").Get<PrintAssetDefns[]>();
或者你可以獲得所有數據
PrintJob[] myArray = _config.GetSection("Production:PrintJobs").Get<PrintJob[]>();
類
public class PrintAssetDefns
{
public string Name { get; set; }
public string SearchPath { get; set; }
public string PrintAssetType { get; set; }
public List<string> ValidExtensions { get; set; }
public List<string> CreativeCodes { get; set; }
}
public class PrintJob
{
public string SalesCategory { get; set; }
public int JobType { get; set; }
public int SubJobType { get; set; }
public int ShipVia { get; set; }
public int CSR { get; set; }
public int SLAHours { get; set; }
public int DaysToArrive { get; set; }
public List<string> Note { get; set; }
public List<PrintAssetDefns> GroupAssets { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.