python json

1.将字典转化为json

json.dumps()

>>> import json
>>> dict = {'name':'zhaolibin','age':20,'sex':'boy'}
>>> json_data = json.dumps(dict,indent=4,separators=(',',': '),sort_keys=True)
>>> print json_data
{
    "age": 20,
    "name": "zhaolibin",
    "sex": "boy"
}

2.json.loads

>>> jsonData = '{"number": 6, "name": "Pythontab"}'
>>> str = json.loads(jsonData)
>>> print str
{u'number': 6, u'name': u'Pythontab'}
>>> print type(str)
<type 'dict'>

  

posted @ 2017-07-03 10:12  悉达多尊  阅读(130)  评论(0编辑  收藏  举报