none
Unable to bind json file with classes on ASP.NET Core RRS feed

  • Question

  • Hi,

    I'm trying to bind a json file to classes in my ASP.NET core project and I only get null in my object.

    The reading from the file is OK since I can't see the values in the config object, but it doesn't bind with my class.

    Where's the problem?

    So here's the json file:

    {
      "Center": {
        "Location": [
          {
            "Name": "GLOBULE de Brossard - Quartier Dix30",
            "Adress": "9370, boulevard Leduc, local 30",
            "City": "Brossard",
            "Prov": "Québec",
            "CP": "J4Y 0B3",
            "Lundi": "10 h à 18 h",
            "Mardi": "10 h à 18 h",
            "Mercredi": "10 h à 18 h",
            "Jeudi": "10 h à 20 h",
            "Vendredi": "10 h à 20 h",
            "Samedi": "9 h à 17 h",
            "Dimanche": "10 h à 17 h",
            "Lat": "45.4440401",
            "Long": "-73.43603089999999"
          },
          {
            "Name": "GLOBULE de Laval - Centre Laval",
            "Adress": "1600, boulevard Le Corbusier (entrée 4)",
            "City": "Laval",
            "Prov": "Québec",
            "CP": "H7S 1Y9",
            "Lundi": "10 h à 18 h",
            "Mardi": "10 h à 18 h",
            "Mercredi": "10 h à 18 h",
            "Jeudi": "10 h à 20 h",
            "Vendredi": "10 h à 20 h",
            "Samedi": "9 h à 17 h",
            "Dimanche": "10 h à 17 h",
            "Lat": "45.5603711",
            "Long": "-73.7299956"
          }
        ]
      } 
    }
    


    The class:

    public class CenterLocations
        {
            public Center Location { get; set; }
    
    
        }
    
        public class Center
        {
            public Location[] Locations { get; set; }
        }
    
        public class Location
        {
            string Name { get; set; }
            string Adress { get; set; }
            string City { get; set; }
            string Prov { get; set; }
            string CP { get; set; }
            string Lundi { get; set; }
            string Mardi { get; set; }
            string Mercredi { get; set; }
            string Jeudi { get; set; }
            string Vendredi { get; set; }
            string Samedi { get; set; }
            string Dimanche { get; set; }
            string Lat { get; set; }
            string Long { get; set; }
        }

    The startup.cs

     var config = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("CenterLocations.json")
                    .Build();
    
    
               services.AddOptions();
                services.Configure<CenterLocations>(config);
                services.AddSingleton(config);

    And the object is null in the controller:

     public class MapController : Controller
        {
    
            private readonly CenterLocations _centers;
         
            public MapController(IOptions<CenterLocations> options)
            {
                _centers = options.Value; //Locations = null
               
    
            }
            public IActionResult Index()
            {
    
                        
                
    
    
                return View();
            }
        }

    • Moved by Sabah ShariqMVP Thursday, March 8, 2018 12:32 PM Moved From Visual C#
    Thursday, March 8, 2018 11:35 AM

All replies

  • Hi CaptnKebec,

    This is Visual C# forum. As your issue is related to Web please ask your question into ASP.Net forum for getting quick response. I am moving your thread to off topic.

    https://forums.asp.net/

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Thursday, March 8, 2018 12:32 PM
  • That forum doesn't seems to be on the MSDN platform, I'm I getting the same support level on that forum since I'm a MSDN subscriber?
    Thursday, March 8, 2018 12:51 PM
  • Yeah, you have to create a new account to post question there. That forum is related to ASP.Net topics.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, March 12, 2018 5:05 AM