当前位置:网站首页>JSON file handles object Tags
JSON file handles object Tags
2022-06-12 20:32:00 【User 2200417】
json Document processing :
What is? json:
JSON(JavaScript Object Notation, JS Object tag ) Is a lightweight data exchange format . It's based on ECMAScript (w3c To formulate the js standard ) A subset of , Use text format completely independent of programming language to store and represent data . A simple and clear hierarchy makes JSON Become the ideal data exchange language . Easy to read and write , At the same time, it is also easy for machine analysis and generation , And effectively improve the network transmission efficiency .Pycharm
JSON Support data format :
- object ( Dictionaries ). Use curly braces .
- Array ( list ). Use square brackets .
- plastic 、 floating-point 、 Boolean types also have null type .
- String type ( The string must be in double quotation marks , You can't use single quotes ).
Use commas to separate multiple data . Be careful :json It's essentially a string .
Dictionary and list transfer JSON:
import json
books = [
{
'title': ' How steel is made ',
'price': 9.8
},
{
'title': ' A dream of red mansions ',
'price': 9.9
}
]
json_str = json.dumps(books,ensure_ascii=False)
print(json_str) because json stay dump When , Only store ascii The characters of , Therefore, Chinese will be escaped , At this time, we can use ensure_ascii=False Turn off this feature . stay Python in . Only basic data types can be converted to JSON Format string . That is to say :int、float、str、list、dict、tuple.
take json Data direct dump To a file :
json In addition to dumps function , One more dump function , This function can pass in a file pointer , Directly string dump To a file . The sample code is as follows :
books = [
{
'title': ' How steel is made ',
'price': 9.8
},
{
'title': ' A dream of red mansions ',
'price': 9.9
}
]
with open('a.json','w') as fp:
json.dump(books,fp)Will a json character string load become Python object :
json_str = '[{"title": " How steel is made ", "price": 9.8}, {"title": " A dream of red mansions ", "price": 9.9}]'
books = json.loads(json_str,encoding='utf-8')
print(type(books))
print(books)Read directly from file json:
import json
with open('a.json','r',encoding='utf-8') as fp:
json_str = json.load(fp)
print(json_str)边栏推荐
猜你喜欢

阿里前辈给我推荐的软件测试人员必读书籍(附电子书),让我受益匪浅...

EditText control starts from the upper left corner

Centos7 installing PHP

View 的事件分发机制

跳槽前恶补面试题,金三成功上岸腾讯,拿到30k的测开offer

20 shortcut keys for vs code!
![[leetcode] small thinking of optimal division](/img/da/ea01225047158c9ca53ac1c585bcd9.jpg)
[leetcode] small thinking of optimal division

How to determine fragment restored from Backstack

Alipay payment episode 11: monitoring after successful payment callback

Explain
随机推荐
1. Getting to know R
Macro definitions and functions
Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?
阿里前辈给我推荐的软件测试人员必读书籍(附电子书),让我受益匪浅...
UVa11991 Easy Problem from Rujia Liu
DFT learning notes
How to download putty using alicloud image?
Halcon angle and radian interchange
QT pro文件配置ffmpeg宏
对闭包的理解
Kyma application connectivity feature introduction
使用Swagger生成 API 文档(go语言示例)
Solve NPM compilation times node_ modules/optipng-bin/vendor/optipng ENOENT
Why my order by create_ Time ASC becomes order by ASC
A Zhu and Xu Baobao's high-rise game - difference
MySQL - the execution order of an SQL statement
Bsn-ddc basic network introduction, technical features, unique advantages, application scenarios and platform access
Alipay payment Episode 12: Crazy God and Feige Alipay payment configuration code (free resources, no thanks for taking them away)
没有学历,自学软件测试,找到一份月入过万的测试工作真的有可能吗?
牛客网:三数之和