Python 官方文档:入门教程 => 点击学习
一、Dictionary 转为JSON 将dict转为JSON,这里利用包json import json aitem = {} aItem["id"] = "2203" aItem["title"] = "title" aItem["
一、Dictionary 转为JSON
import json
aitem = {}
aItem["id"] = "2203"
aItem["title"] = "title"
aItem["subTitle"] = "sub title"
bItem = {}
bItem["id"] = "2842"
bItem["title"] = "b标题"
bItem["subTitle"] = "b副标题"
bItem["content"] = "内容"
bItem["list"] = ["a", "a 2", "b", "bb"]
aJson = json.dumps(aItem)
bJson = json.dumps(bItem, ensure_ascii=False)
print(aItem)
print(aJson)
print(bJson)
涉及到中文字符的时候,需要指定ensure_ascii=False
输出:
{'id': '2203', 'title': 'title', 'subTitle': 'sub title'}
{"id": "2203", "title": "title", "subTitle": "sub title"}
{"id": "2842", "title": "b标题", "subTitle": "b副标题", "content": "内容", "list": ["a", "a 2", "b", "bb"]}
二、list 转为JSON
接上面的代码
jsonList = []
jsonList.append(aItem)
jsonList.append(bItem)
jsonArr = json.dumps(jsonList, ensure_ascii=False)
print(jsonArr)
输出:
[{"id": "2203", "title": "title", "subTitle": "sub title"}, {"id": "2842", "title": "b标题", "subTitle": "b副标题", "content": "内容"}]
--结束END--
本文标题: python 列表 字典转json
本文链接: https://lsjlt.com/news/191650.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0