It projects use JSON format data to save project configuration information, and then read the contents of the JSON file in the configuration file to assign values

Source: Internet
Author: User

JSON format is small and small, suitable for configuration files, especially in large projects, you can classify the configuration information into different JSON files, and then read the configuration file in the configuration file data to be assigned, here in Python as an example to illustrate:

Suppose you save the following information in the Yourfile.json file:

{    "User_message":{        "name":"Lowman",        " Age":" -",    },    "Database":{        "USER":"Root",        "Passwor":"123",    }}

To use JSON in Python, you need to reference JSON:

Try :     Import JSON except importerror:     import Simplejson as JSON

This reference uses the try except to refer to JSON from Simplejson if the reference JSON fails. The former performs better, but some systems may not have this package installed.

With open ("The path of your JSON File/yourfile.json", "R") as Jsonfile:    = jsonfile.read ()    = JSO N.loads (Val) # It should also be possible to use the Eval () function, Messageconfig = eval (val)

The above code first opens the file with open, then reads the contents into the Val variable, and then uses the json.loads(val) method to convert the string to a Python Dictionary object so that it can be used in the program, using the following method (need to get data based on the actual JSON file data structure area)

Mes_config = messageconfig["user_message"= mes_config["name"]
Age = mes_config["Age"]

It projects use JSON format data to save project configuration information, and then read the contents of the JSON file in the configuration file to assign values

Related Article

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.