Home > Mobile >  Getting Array inside of an Array from json ConfigFile using Configuration Builder in C#
Getting Array inside of an Array from json ConfigFile using Configuration Builder in C#

Time:08-13

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

        }    
    }
}

CodePudding user response:

In case of console application you need to do it manually for every recipient and assign to your data object:

  Recipients = config.GetSection("Recipeints").Get<List<Recipient>>();
  for (var index = 0; index < Recipients.Count;   index)
  {
      ApiSettings[index].aboba = config.GetSection($"Recipeints:{index}:Units").Get<Unit>();
  }

BUT, as for me that`s not appsettings, that must be json file with data, which you can serialize/deserialize in easy way and what will have more sense

CodePudding user response:

Try the following

{
  "Recipients": [
    {
      "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" ]
          }
        ]
      }
    },
    {
      "MemberNo": "xxxx",
      "Data": {
        "ShortName": "BCompany",
        "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": [ "D123", "D234", "D345", "D111" ]
          }
        ]
      }
    }
  ]
}

Each class should be in their own file.

using System;
using System.Linq;
using Microsoft.Extensions.Configuration;

namespace Demo
{
    partial class Program
    {
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            var configuration = builder.Build();
            var recipients = configuration.GetSection("Recipients").Get<Recipient[]>();


            var dictionary = recipients.ToDictionary(r => r.Data.ShortName, r => r);

            foreach ((string key, Recipient value) in dictionary)
            {
                Console.WriteLine(key);
                foreach (var unit in value.Data.Units)
                {
                    Console.WriteLine($"\t{string.Join(",", unit.CustNos)}");
                }
            }

            Console.ReadLine();
        }
    }
    
    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 Deliminator { 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 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; }
    }

}

enter image description here

  • Related