json.dumps、json.loads以及jsonpath解释举例
json.dumps :把字典转换成字符串
#在python中字典就是json对象
import json
str_dict={"name":"xiaoming","age":"18"} #json.dumps :把字典转换成字符串
str1=json.dumps(str_dict)
print(type(str1))
json.loads :把字符串转换成字典
str2='{"name":"xiaoming","age":"18"}' #json.loads :把字符串转换成字典
str_json=json.loads(str2)
print(type(str_json))
print(str_json['age'])
jsonpath用处:对json数据进行解析
# jsonpath对json数据解析
import jsonpath
jsondata={"tags":[{"id":2,"name":"星标组","count":0},{"id":100,"name":"冰淇淋","count":0},{"id":101,"name":"百事可乐","count":0},{"id":102,"name":"衡阳","count":1},{"id":103,"name":"上海777","count":0}]}
value=jsonpath.jsonpath(jsondata,'$.tags[2].name')
print(value[0])