|
你看看代码吧,随便写了个例子。就是说嘛dict与string的互转
- def qatest():
- # 一个dict,对应为json对象
- json_obj = {
- "name": "Tom",
- "age": 20
- }
- # 一个string,但内容同样可以转换为json格式
- json_str = '{"name":"Jack", "age": 25}'
- # dict转string
- print json_obj, type(json_obj)
- json_obj_str = json.dumps(json_obj)
- print json_obj_str, type(json_obj_str)
- # string转dict
- print json_str, type(json_str)
- json_str_obj = json.loads(json_str)
- print json_str_obj, type(json_str_obj)
- {'age': 20, 'name': 'Tom'} <type 'dict'>
- {"age": 20, "name": "Tom"} <type 'str'>
- {"name":"Jack", "age": 25} <type 'str'>
- {u'age': 25, u'name': u'Jack'} <type 'dict'>
- [Finished in 0.0s]
复制代码 |
|