龙空技术网

json与string转化——Python语法

知识淘金 102

前言:

如今姐妹们对“python string 转dict”大概比较关怀,看官们都需要学习一些“python string 转dict”的相关文章。那么小编在网上搜集了一些对于“python string 转dict””的相关资讯,希望大家能喜欢,各位老铁们一起来学习一下吧!

import json

import chardet

data = {

"statusCode": 200,

"data": {

"totoal": "6",

"height": "10.1",

"weight": "10.50",

"age": "12"

},

"msg": "成功"

}

1.1 #dict转化str

s = json.dumps(data,ensure_ascii=False)

加这个参数ensure_ascii=False为了转化utf-8

1.2 #解析

chardet.detect(json.dumps(data,ensure_ascii=False).encode())

{'encoding': 'utf-8', 'confidence': 0.7525, 'language': ''}

2.1 读取文件中json形式的字符串元素 转化成python类型

s1 = json.loads(s)

print (s1,type(s1))

标签: #python string 转dict