. Netcore getting JSON file configuration content

Source: Internet
Author: User

The data configuration and content in the. Netcore uses a JSON file instead of an XML file from the previous framework, so how do you get the data in JSON? Here's how to get the JSON file in. NET Core.

First, create a new. NET core Web application, create a new folder Jsonfile, and create a new Class.json file in the Jsonfile folder.

  

Right-Class.json, set assignment to output directory to: Always copy (very important).

  

Then add the content in the Class.json:

{  "Classnum":"1",  "ClassName":"high One by one classes",  "Students": [    {      "name":"Obama",      " Age": -    },    {      "name":"Putin",      " Age": -    }  ]}

Right-click Project dependencies: Manage Guget packages, add Microsoft.Extensions.Configuration assemblies to your project

  

Finally, add the relevant code to get the JSON file data in the home controller:

namespace coredemo.controllers{public    class Homecontroller:controller    {public        iactionresult Index ()        {            var builder = new Configurationbuilder (). Addjsonfile ("Jsonfile/class.json");            var config = builder. Build ();            String str = "class:" + config["Classnum"]+ "/";            Return Content ("class number =" +config["Classnum"]+ ", class name =" + config["ClassName"]+ "\ r"                + "Student name =" +config["students:0: Name "]+", student age = "+ config[" Students:0:age "]            );}}}    

Run the project, display the results of the run, the data has been read out and displayed in the browser, indicating that the ability to get the JSON file configuration content has been implemented:

  

. Netcore getting JSON file configuration content

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.