Json_python学习笔记

1. Json基础

  • JSON的布尔值
    • {“flag”:true}
    • {“flag”:false}
  • JSON的null
    • {“name”:null}
  • JSON对象
    • json对象是用大括号{}围起来的
    • {“name”:“tom”, “age”:20}
  • JSON数组
    • json数组用中括号[]围起来
    • [
      {“name”:“tom”, “age”:20},
      {“name”:“jerry”, “age”:21},
      {“name”:“dog”, “age”: 19}
      ]
  • json文件
    • json的文件类型是 .json
    • json的MIME类型是application/json

2. Python类型与Json类型的转换

2.1 Python类型转Json类型的规则

PythonJson
dict字典object对象
list列表、tuple元组array数组
strstring
Truetrue
Falsefalse
Nonenull

2.2 Json类型转Python类型的规则

JsonPython
object对象dict字典
array数组list列表
stringstr
trueTrue
falseFalse
nullNone

2.3 Python字典与Json对象的互转

  • python的json模块可以对json进行解码编码
  • 字典转JSON对象,使用json.dumps(dict_data)
import json

dict_data = {
	'a':1,
	'b':2,
	'c':3,
	'd':None,
	'e':True,
	'f':False,
	'g':('xxx', 'yyy', 'zzz'),
	'h': ['mmm', 'nnn', 'ppp']
}

print(type(dict_data)) # <class 'dict'>

# python字典转Json对象
json_data = json.dumps(dict_data)
print(type(json_data)) # 打印出来的是<class 'str'>
print(json_data) # 位置1

# json对象转python字典
dict_new = json.loads(json_data)
print(type(dict_new)) # <class 'dict'>
print(dict_new) # 位置2
  • 位置1打印的如下
    • 字典中的None已经转成了json的null
    • 字典中的True、False已经转成了Json中的true和false
    • 字典中元组和列表,已经转成了Json中的数组

{
“a”:1,
“b”:2,
“c”:3,
“d”:null,
“e”:true,
“f”:false,
“g”:[“xxx”, “yyy”, “zzz”],
“h”: [“mmm”, “nnn”, “ppp”]
}

  • 位置2打印的是
    • 可以看到,此时json中的数组g,没法转回元组了,只能转成列表

{
‘a’:1,
‘b’:2,
‘c’:3,
‘d’:None,
‘e’:True,
‘f’:False,
‘g’:[‘xxx’, ‘yyy’, ‘zzz’],
‘h’: [‘mmm’, ‘nnn’, ‘ppp’]
}

2.4 Python列表与Json数组的互转

  • 使用json.dumps(list_data),把python列表转成Json数组
  • 使用json.loads(json_data),把json转成python的列表
import json

list_data = [
	{'a': 1, 'b': 2, 'c': 3},
	{'name': 'tom', 'xx': 2, 'yy': 33}
]
print(type(list_data)) # <class 'list'>

# python的列表转json
json_data=json.dumps(list_data)
print(type(json_data)) # <class 'str'>
print(json_data) # [{"a": 1, "b": 2, "c": 3}, {"name": "tom", "xx": 2, "yy": 33}]

# json转成python的列表
list_new = json.loads(json_data)
print(type(list_new)) # <class 'list'>
print(list_new)	# [{'a': 1, 'b': 2, 'c':3}, {'name': 'tom', 'xx': 2, 'yy': 33}]

3. 处理Json文件

  • dumps()和loads()用于处理json字符串
  • dump()和load()用于处理json文件

3.1 从Json文件读取Json对象

a.json是一个json文件,里面是一个json对象

{
“a”:1,
“b”:2,
“c”:3,
“d”:null,
“e”:true,
“f”:false,
“g”:[“xxx”, “yyy”, “zzz”]
}

  • 从Json文件中读取Json对象,用data = json.load(f)
import json

# 读取Json文件中的Json对象
with open('a.json', 'r') as f:
	data = json.load(f)
	print(type(data)) # 打印<class 'dict'>
	print(data) # 位置1
	print(type(data['g'])) # 打印<class 'list'>
  • 位置1打印的是

{
‘a’:1,
‘b’:2,
‘c’:3,
‘d’:None,
‘e’:True,
‘f’:False,
‘g’:[‘xxx’, ‘yyy’, ‘zzz’]
}

  • 读进来的data已经是字典类型了,可以看到data[‘g’]是列表类型
  • a.json文件里的null,读进来的是None
  • a.json文件里的true、false,读进来的是True、False

3.2 从Json文件读取Json数组

b.json文件里的是一个Json数组

[
{“a”:1,“b”:2, “c”:3},
{“d”:4,“e”:5,“f”:6}
]

  • 从Json文件中读取Json数组,用data = json.load(f)
import json

# 读取Json文件中的Json数组
with open('b.json', 'r') as f:
	data = json.load(f)
	print(type(data)) # <class 'list'>
	print(data) # [{'a':1, 'b':2, 'c':3}, {'d':4, 'e':5, 'f':6}]

3.3 往文件里写入Json对象

  • 往文件里写入json对象,用json.dump(data, f)
import json

# 直接在python中的字典里,写null、true、false会报错,得写成None、True、False
data = {
	"a":1,
	"b":2,
	"c":3,
	"d":None,
	"e":True,
	"f":False,
	"g":("xxx", "yyy", "zzz")
}

# 将json对象写入json文件
with open('b.json', 'w') as f:
	json.dump(data, f)
  • 运行后,b.json文件中是

{
“a”:1,
“b”:2,
“c”:3,
“d”:null,
“e”:true,
“f”:false,
“g”:[“xxx”, “yyy”, “zzz”]
}

  • 此时,python字典里的None、True、False,都已经转成了b.json文件里的null、true、false

3.4 往文件里写入Json数组

import json

data = [
	{'a':1, 'b':2, 'c':3},
	{'d':4, 'e':5, 'f':6}
]

# 把Json数组写入Json文件
with open('b.json', 'w') as f:
	json.dump(data, f)
  • 此时b.json文件里的数据是

[
{“a”:1, “b”:2, “c”:3},
{“d”:4, “e”:5, “f”:6}
]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值