描述
json2dict() 将json格式的字符串转dict或将dict数据转成json字符串、读取json文件
json2dict() 接收四个参数,第一个参数为要转换的数据,第二个参数为文件url,第三个参数为是否转义特殊字符,第四个参数为是否强制将str转成list或tuple。
语法
以下是 json2dict() 的语法:
1 | json2dict(data=None, file=None, trans=True, force=False) |
参数
● data [json|dict]:需要转换的数据。
● file [str]:文件的url。
● trans [bool]:是否转义特殊字符。
● force [bool]:是否强制将str转list或tuple。
返回值
返回一个字典或字符串类型的数据
实例
以下展示了使用 json2dict() 的实例:
1 2 3 4 5 | from kyger.utility import json2dictprint(json2dict(file='template/frontend/default/config.json')) # json文件print(json2dict('{"name": "kgcms", "age": 25}')) # json转字典print(json2dict({"name": "kgcms", "age": 25})) # 字典转jsonprint(json2dict('https://www.kgcms.com//api?action=get_web_info')) # API接口获取json转字典 |
以上实例运行后输出的结果为:
1 2 3 4 | { "tid": "kg001", /*模板识别码,英文、数字或下划线组成,不得含有中文,不得与其它模板识别码重复*/ "name": "系统默认模板", /*模板名称*/ "production": "KGCMS", /*模板作者*/ "website": "http://www.mlecms.com", /*作者网址*/ "version": "1.0", /*模板适用版本*/ "date": "2019-08-20", /*模板发布时间*/ "thumbnail": "images/preview.jpg", /*模板缩略图,相对于当前模板目录 120 * 160px*/ "type": "['auto','pc','wx']" /* 模板类型: auto|pc|wx|m 自适应|pc|wx|手机版 */ }{'name': 'kgcms', 'age': 25}'{\"name\": \"kgcms\", \"age\": 25}'{"name": "kgcms", "www": "http://www.kgcms.com"} |
适应版本
v1.0.1








