How to read values from config.json in Console Application

后端 未结 4 2209
南旧
南旧 2021-02-15 08:54

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:24

    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 *

提交回复
热议问题