[. NET Core] simply reads the json configuration file, corejson
Simple reading of the background of the json configuration file
Currently, it is difficult to read configuration files from. NET Core on the Internet.
. NET Core does not use xml configuration files such as app. config and web. config. It uses json format to store configuration file information.
{"Name": "wen", "age": 26, "family": {"mother": {"name": "Niang", "age": 55 }, "father": {"name": "Dad", "age": 56 }}}
Nuget Class Library Reference
Two Nuget class libraries are required:
① Microsoft. Extensions. Configuration
② Microsoft. Extensions. Configuration. Json
Core code
Program. cs:
Using System; using System. IO; using Microsoft. extensions. configuration; namespace Demo {class Program {static void Main (string [] args) {// Add the json file path var builder = new ConfigurationBuilder (). setBasePath (Directory. getCurrentDirectory ()). addJsonFile ("deleteworkflow. json "); // create the configuration root object var configurationRoot = builder. build (); // obtain the name part of the configuration root var nameSection = configurationRoot. getSection ("name"); // take the family section under the configuration root var familySection = configurationRoot. getSection ("family"); // take the name section var motherNameSection = familySection under the mother section of the family section. getSection ("mother "). getSection ("name"); // take the age section var fatherAgeSection = familySection under the father section under the family section. getSection ("father "). getSection ("age"); // Value is the text Value Console. writeLine ($ "name: {nameSection. value} "); Console. writeLine ($ "motherName: {motherNameSection. value} "); Console. writeLine ($ "fatherAge: {fatherAgeSection. value} "); Console. read ();}}}
Test results:
The intuitive link comparison graph shows that the core is the GetSection () method. The GetSection () method is called again every time you continue to obtain the link at the next level:
Remarks
Don't forget to set attributes of the json file: