-1

I'm trying to read JSON file from the my local folder and create objects send it to view and also trying to read the json file and create the classes dynamically (i.e I want to create class after based on json file what I get I searched a lot about it somebody please help) my json

{

  "Dev": {
            "resourcegroup": "devusemap",
            "storageaccount": "devusemdapstorage",
            "azuresqlserver": "azuresqlserver",
            "container": "container",
            "sourcesqlserver": "sourcesqlserver"
         },
  "qa": {
            "resourcegroup": "qavusemap",
            "storageaccount": "qavusemdapstorage",
            "azuresqlserver": "azuresqlserver",
            "container": "container",
            "sourcesqlserver": "sourcesqlserver"
        }

}

my class

 public class Environmet
    {
        public string Name { get; set; }

        Env_properties prop { get; set; }
    }
 public class Env_properties
    {
        public string resourcegroup { get; set; }
        public string storageaccount { get; set; }
        public string azuresqlserver { get; set; }
        public string container{ get; set; }
        public string sourcesqlserver { get; set; }
    }

my controller

 public IActionResult Env()
        {
            var json = System.Path("wwwroot/data/sample.json");
            var json = System.IO.ReadAllText(Server.MapPath("wwwroot/data/sample.json"));
            return View();
        }
  • 1
    Does this answer your question? [Deserialize JSON to C# Classes](https://stackoverflow.com/questions/25052293/deserialize-json-to-c-sharp-classes) as well as [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string) – Pavel Anikhouski May 15 '20 at 14:15

1 Answers1

-1

based on you current JSON the classes have to look like:

public class Dev
{
    public string resourcegroup { get; set; }
    public string storageaccount { get; set; }
    public string azuresqlserver { get; set; }
    public string container { get; set; }
    public string sourcesqlserver { get; set; }
}

public class Qa
{
    public string resourcegroup { get; set; }
    public string storageaccount { get; set; }
    public string azuresqlserver { get; set; }
    public string container { get; set; }
    public string sourcesqlserver { get; set; }
}

public class Example
{
    public Dev Dev { get; set; }
    public Qa qa { get; set; }
}

i generated them based on you JSON here

deserializing of your input JSON with the generated classes would look like:

var obj = JsonSerializer.Deserialize<Example>(json );
Daniel
  • 9,491
  • 12
  • 50
  • 66
  • hii can you help how to crate c# class from son schema using NJsonSchema.CodeGeneration. i mean how how to read the json shema from file and create the class. i have gone through many of the document not understand things. please add some code snipset for the same. thank you – chethan kumar May 21 '20 at 10:55