当前位置:   article > 正文

python中的json数据转换_python中json转成=

python中json转成=

例子

import json

# 将【列表】转换为json数据
data = [{"name": "张三", "age": 22}, {"name": "李四", "age": 21}, {"name": "王五", "age": 32}]
# ensure_ascii=False 表明不用ascii编码,而使用unicode编码,这样可显示中文
json_str = json.dumps(data, ensure_ascii=False)
print(type(json_str))
print(json_str)

# 将【字典】转换为json数据
d = {"name": "陈奕迅", "song": "苦瓜"}
json_str = json.dumps(d, ensure_ascii=False)
print(type(json_str))
print(json_str)

# 将json字符串转换成 python 数据类型 list
s = '[{"name": "张三", "age": 22}, {"name": "李四", "age": 21}, {"name": "王五", "age": 32}]'
l = json.loads(s)
print(type(l)) # list
print(l)

s = '{"name": "陈奕迅", "song": "苦瓜"}'
l = json.loads(s)
print(type(l)) # dict
print(l)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

输出:

<class ‘str’>

[{“name”: “张三”, “age”: 22}, {“name”: “李四”, “age”: 21}, {“name”: “王五”, “age”: 32}]

<class ‘str’>

{“name”: “陈奕迅”, “song”: “苦瓜”}

<class ‘list’>

[{‘name’: ‘张三’, ‘age’: 22}, {‘name’: ‘李四’, ‘age’: 21}, {‘name’: ‘王五’, ‘age’: 32}]

<class ‘dict’>

{‘name’: ‘陈奕迅’, ‘song’: ‘苦瓜’}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/171221
推荐阅读
相关标签
  

闽ICP备14008679号