I have nothing to do. Let's learn how to use it with you json~
what are you having? python I won't answer the related error report 、 Or source code information / Module installation /
Women's clothing bosses are proficient in skillsYou can come here :(https://jq.qq.com/?_wv=1027&k=2Q3YTfym) perhaps +V:python10010 Ask me
See resources , It should look like the bottom :
{
"name": "dabao",
"id":123,
"hobby": {
"sport": "basketball",
"book": "python study"
}
}
Does the subject look like Python My dictionary is very much like !
Yes , When you read python In the time , It's just a dictionary .
Dictionary value Yes, it can be completely determined by the user , It can be int, It can be float, It can also be str Or a list .
Read json The operation is as follows :
( Be careful :json It is to be read in the way of reading text ! This reveals the essence ,json It's essentially a formatted text file ! It often consists of utf-8 code , Not binary )
import json
with open(' route ','r', encoding='utf8') as fp:
json_data = json.load(fp)
print(' This is the... In the file json data :',json_data)
print(' This is the data type that reads the file data :', type(json_data))
( The code here may also be GB code ?)
What comes in is a dictionary .
python Learning exchange group :660193417###
import json
a = {
"name": "dabao",
"id":123,
"hobby": {
"sport": "basketball",
"book": "python study"
}
}
b = json.dumps(a)
with open('new_json.json', 'w') as fp:
fp.write(b)
First, through json.dumps() hold dict Demote to string .
Then write the string to json In file . It's that simple .