程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

What should a JSON look like? How does Python use JSON?

編輯:Python

Hey, everyone, good duck , I'm a panda

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 skills You can come here :(https://jq.qq.com/?_wv=1027&k=2Q3YTfym) perhaps +V:python10010 Ask me

First , One json What does it look like ?

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 .

How to write into json file ?

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)

The effect is as follows :

First, through json.dumps() hold dict Demote to string .

Then write the string to json In file . It's that simple .

Today's knowledge point is like this , I hope it will be helpful to you who are learning by yourself ~

I'm a panda , See you in the next article (*◡‿◡)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved