首页 > 编程语言 >python基础-json

python基础-json

时间:2023-02-26 18:57:36浏览次数:36  
标签:name python age 基础 json str print type

import json

# 准备列表,列表内每一个元素都是字典,将其转为JSON
date = [{"name": "张大帅", "age": 11},
        {"name": "王大锤", "age": 13},
        {"name": "赵啸虎", "age": 16}
        ]
# print(date)
# print(type(date))
json_str = json.dumps(date,ensure_ascii=False)
print(type(json_str))
print(json_str)


#转变字典,将字典转换为JSON
d = {"name":"周杰伦","addr":"台北"}
json_str = json.dumps(d,ensure_ascii=False)
print(type(json_str))
print(json_str)

#将JSON字符串转换为python数据类型[{k:v},{k:v},{k:v},{k:v}]
s = '[{"name": "张大帅", "age": 11},{"name": "王大锤", "age": 13},{"name": "赵啸虎", "age": 16}]'
l = json.loads(s)
print(type(l))
print(l)

#将JSON字符串转换为python数据类型 {k:v,k:v}
s = '{"name":"周杰伦","addr":"台北"}'
d = json.loads(s)
print(type(d))
print(d)

 

标签:name,python,age,基础,json,str,print,type
From: https://www.cnblogs.com/Wesuiliye/p/17157286.html

相关文章