I am unable to access the "Units" Array in the json file. I have tried pulling the Json in different ways but it does not come in. I if I myConfig.GetSection("CatalogData:Recipeints") or do a myConfig.GetSection("CatalogData:Recipeints").GetChildren() I get no values, or keys. I figure it is because I am new to this. I can get the Recipeints Array just fine. But the Units is what comes up blank.
appSetting.json
"Recipeints": [
{
"MemberNo": "xxxx",
"Data": {
"ShortName": "ACompany",
"StoredProc": "unknown",
"FileName": "unknown",
"Deliminator": "~",
"EmailFile": 0,
"EmailAddress": "[email protected]",
"FTPFile": 1,
"FTPInfo": {
"FTPName": "ftp.something.com",
"User": "user",
"Password": "password"
},
"FileDrop": 1,
"FileDropLocation": "\\\\FTPServer\\data\\ACompany\\outbound\\",
"FileOrder": "MemberNo,VendorItem,Price,Description",
"Units": [
{
"UnitName": "EndCust",
"CustNos": [ "c123","c234","c345","c111" ]
}
]
}
}
]
}
Classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CatalogCreator
{
public class Recipient
{
public string MemberNo { get; set; }
public Data Data { get; set; }
}
public class Data
{
public string ShortName { get; set; }
public string StoredProc { get; set; }
public string FileName { get; set; }
public string Deliminatior { get; set; }
public int EmailFile { get; set; }
public string EmailAddress { get; set; }
public int FTPFile { get; set; }
public FTPInfo FTPInfo { get; set; }
public int FileDrop { get; set; }
public string FileDropLocation { get; set; }
public string FileOrder { get; set; }
public List<Unit> Units { get; set; }
}
public class FTPInfo
{
public string FTPName { get; set; }
public string User { get; set; }
public string Password { get; set; }
}
public class Unit
{
public string UnitName { get; set; }
public string CustNos { get; set; }
}
}
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CatalogCreator
{
public static class CatalogData
{
class CData
{
public List<Recipient> Recipients;
}
private static List<Recipient> _Recipients = new List<Recipient>();
public static List<Recipient> Recipients
{
get
{
if (_Recipients.Count == 0) { Setup(); }
if (_Recipients.Count == 0)
{
List<Recipient> r = new List<Recipient>();
r.Add(new Recipient());
return r; ;
}
else
{
return _Recipients;
}
}
set { _Recipients = value; }
}
public static void Setup()
{
//from the Settings
IConfiguration myConfig = new ConfigurationBuilder()
.AddJsonFile("appSettings.json")
.AddEnvironmentVariables()
.Build();
//var rep = myConfig.GetRequiredSection("CatalogData:Recipeints").Get<List<Recipient>>(); //.GetSection("Recipeints").GetChildren().AsEnumerable<List<Recipient>>();
//var repRec = myConfig.GetRequiredSection("CatalogData").GetChildren();
////var repArray = repRec.AsEnumerable().ToList();
//rep.ForEach(r => {
// _Recipients.Add(r);
// });
var rep = myConfig.GetSection("CatalogData:Recipeints");
List<Recipient> recipients = rep.Get<List<Recipient>>();
//Dictionary<string, Recipient> data = new Recipient.ToDictionary(rep => rep.name, rep => rep);
}
}
}
