简体   繁体   中英

How to load JSON file and assign a value from this JSON to a local variable in Python code?

I have a JSON file ( config.json ) which looks like this:

{
  "name":"Ram"
}

And a script looks like this:

f=open('config.json',)
name=json.load(f) 
print(name)

Then, how to assign the local variable (ie, name) into Python code?

You would treat it like any other dictionary. myDictionary[myKey] would equal myValue for a JSON like {myKey: myValue} . Your code should look like this:

f=open('config.json',)
jsonF=json.load(f)
name = jsonF["name"] #THIS is how to get the value from a key in a dictionary
print(name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM