[. NET Core] simply reads the json configuration file, corejson

Source: Internet
Author: User

[. 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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.