I have 1 JSON as followsmyObj_1 = name runoob alexa10000 sites site1 wwwrunoobcom site2 mrunoobcom site3 crunoobcom There is also 1 JSON file myObj_2json as followsm
The reason why Code 3 and Code 4 cannot be used to query the value of "site2" in myObj_2 is because myObj_2 is a JSON file and needs to be loaded before it can be accessed. Therefore, the correct way to query the value of "site2" in myObj_2 would be:
Code 5:
import json
with open('myObj_2.json') as f:
myObj_2 = json.load(f)
x = myObj_2["sites"]["site2"]
Here, we first import the json module and then use the 'open' function to open the JSON file. We then load the contents of the file using the 'json.load' function and assign it to the variable 'myObj_2'. Finally, we can access the value of "site2" using the format 'myObj_2["sites"]["site2"]'
原文地址: https://www.cveoy.top/t/topic/fX3J 著作权归作者所有。请勿转载和采集!