How to read values from config.json in Console Application

后端 未结 4 1031
臣服心动
臣服心动 2021-02-15 08:55

I just installed ASP.NET 5 and created a Console Application in Visual Studio. I\'ve added a file, config.json, to the root folder of the project.

It looks like this:

4条回答
  •  遇见更好的自我
    2021-02-15 09:34

    You can use JavascriptSerializer (namespace: System.Web.Extension) to parse json into dictionary and find any value based on its key from json. You code would become:

     string json = System.IO.File.ReadAllText("PathToJsonFile");
     JavaScriptSerializer serializer = new JavaScriptSerializer();
     Dictionary dic = serializer.Deserialize>(json);
    

    If you iterate over the dictionary, you can see all the keys and get their values using dic.

    * Just an alternate solution *

提交回复
热议问题