python 字符串转 json 把python字典类型转换为 JSON字符串

python \u600e\u6837\u628a\u5b57\u5178\u8f6c\u6210json\u5b57\u7b26\u4e32

\u6709\u4e00\u4e2a\u9700\u6c42\uff0c\u9700\u8981\u7528python\u628ajson\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5b57\u5178
inp_str = " {'k1':123, 'k2': '345',\u2019k3\u2019,\u2019ares\u2019} "
import jsoninp_str = " {'k1':123, 'k2': '345',\u2019k3\u2019,\u2019ares\u2019} "print json.loads(a)

\u6b7b\u6d3b\u51fa\u4e0d\u6765\u7ed3\u679c\uff0c\u8fd8\u62a5\u9519\uff0c\u6ca1\u641e\u660e\u767d\u3002

\u6700\u540e\uff0c\u76f4\u63a5\u590d\u5236\u7f51\u4e0a\u7684\u4ee3\u7801\uff0cOK\uff0c\u8fd0\u884c\u6210\u529f\uff0c\u53ef\u662f\u628a\u6211\u7684inp_srt\u53d8\u91cf\u586b\u8fdb\u53bb\uff0c\u4e0d\u884c\uff0c\u62a5\u9519\uff1b\u5f00\u59cb\u5bf9\u6bd4\u4e24\u4e2a\u53d8\u91cf\u6709\u4ec0\u4e48\u4e0d\u540c\uff0c\u4e00\u76f4\u4ee5\u4e3aPython\u4e2d\u53cc\u5f15\u53f7\u5355\u5f15\u53f7\u53ef\u4ee5\u968f\u4fbf\u7528\uff0c\u610f\u601d\u4e0d\u53d8\uff1b\u6700\u540e\u767e\u5ea6\u4e86\u77e5\u9053json\u7684\u6807\u51c6\u683c\u5f0f\uff1a\u8981\u6c42\u5fc5\u987b \u53ea\u80fd\u4f7f\u7528\u53cc\u5f15\u53f7\u4f5c\u4e3a\u952e \u6216\u8005 \u503c\u7684\u8fb9\u754c\u7b26\u53f7\uff0c\u4e0d\u80fd\u4f7f\u7528\u5355\u5f15\u53f7\uff0c\u800c\u4e14\u201c\u952e\u201d\u5fc5\u987b\u4f7f\u7528\u8fb9\u754c\u7b26\uff08\u53cc\u5f15\u53f7\uff09
\u6539\u4e86\u540eOK\u4e86\u3002
#-*-coding:utf-8-*-import json
inp_strr = '{"k1":123, "k2": "456", 'k3':"ares"}'inp_dict = json.loads(inp_strr) # \u6839\u636e\u5b57\u7b26\u4e32\u4e66\u5199\u683c\u5f0f\uff0c\u5c06\u5b57\u7b26\u4e32\u81ea\u52a8\u8f6c\u6362\u6210 \u5b57\u5178\u7c7b\u578bprint inp_dict

# \u5bfc\u5165\u5305
import json
# \u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u7c7b\u578b\u7684\u6570\u636e
data = { 'id': 1, 'name': 'Tom', 'address': '\u5317\u4eac\u5e02\u6d77\u6dc0\u533a', 'school': None }
# \u5c06\u5b57\u5178\u7c7b\u578b\u8f6c\u6362json\u5b57\u7b26\u4e32
json_str = json.dumps(data)
# \u8f93\u51fa\u7ed3\u679c
print(json_str)
\u8981\u662f\u771f\u60f3\u5b66\uff0c\u63a8\u8350\u4f60\u770b\u9ed1\u9a6c\u7a0b\u5e8f\u5458\u7684\u516c\u5f00\u8bfe\uff0c\u4e0d\u50cf\u522b\u7684\u514d\u8d39\u8bfe\u7a0b\u8bb2\u7684\u90a3\u4e48\u6d45\u663e\uff0c\u5185\u5bb9\u76f8\u5bf9\u6765\u8bf4\u5f88\u826f\u5fc3\u4e5f\u6709\u4e00\u5b9a\u7684\u6df1\u5ea6\u3002\u81ea\u5b66\u7684\u8bdd\u975e\u5e38\u5408\u9002\u3002

json本身就是字符串,是符合json格式的字符串。
所以,你说的,字符串转json,就是不正确的描述。

一般正常的用法是:
涉及到,在json字符串,来自字符变量或文件内容,和不同类型的变量,之间的转换。

变量转json:
json.dumps或json.dump

json转变量:
json.loads或json.load

详解:
【整理】Python中将(字典,列表等)变量格式化成(漂亮的,树形的,带缩进的,JSON方式的)字符串输出
【整理】什么是JSON+如何处理JSON字符串

(此处不给贴地址,请自己用google搜标题,即可找到帖子地址)

python字符串转json对象,需要使用json模块的loads函数,如下所示:
>>> import json
>>> s = '{"skey":"val","ikey":10}'
>>> jo = json.loads(s)
>>> jo
{'ikey': 10, 'skey': 'val'}
>>> jo['ikey']
10
>>> jo['skey']
'val'

json.loads介绍:
json.loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Deserialize s (a str instance containing a JSON document) to a Python object using this conversion table.
The other arguments have the same meaning as in load(), except encoding which is ignored and deprecated.
If the data being deserialized is not a valid JSON document, a JSONDecodeError will be raised.

>>> import json
>>> a = json.loads('{"a":"b"}')
>>> a
{u'a': u'b'}

扩展阅读:python代码自动生成器 ... python endswith ... python 字符串 替换 ... python基础代码大全 ... python 字符串 包含 ... python list转字符串 ... python编程入门自学 ... python中 for i 9 ... python字符串转换为数字 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网