python的json模块的dumps,loads,dump,load方法介绍

Posted F

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python的json模块的dumps,loads,dump,load方法介绍相关的知识,希望对你有一定的参考价值。

 

dumps和loads方法都在内存中转换,
dump和load的方法会多一个步骤,dump是把序列化后的字符串写到一个文件中,而load是从一个文件中读取字符串

 

将列表转为字符串

>>> a=[1,2,3,4,5]
>>> import json
>>> json.dumps(a)
\'[1, 2, 3, 4, 5]\'
>>> type(json.dumps(a))
<class \'str\'>
>>> 

将字典转化为字符串

>>> b={"cc":12,"vv":33}
>>> json.dumps(b)
\'{"cc": 12, "vv": 33}\'
>>> type(json.dumps(b))
<class \'str\'>

将字符串转为列表

>>> f=json.dumps(a)
>>> f
\'[1, 2, 3, 4, 5]\'
>>> json.loads(f)
[1, 2, 3, 4, 5]

将字符串转为字典

>>> k=json.dumps(b)
>>> k
\'{"cc": 12, "vv": 33}\'
>>> json.loads(k)
{\'cc\': 12, \'vv\': 33}
>>> 

 

字符串转字典

#coding=utf-8
import requests
import json
#
ret = requests.get(\'http://wthrcdn.etouch.cn/weather_mini?city=北京\')
ret.encoding = \'utf-8\'
s1 = ret.text
print(s1,type(s1))

d1 = json.loads(s1)
print(d1,type(d1))

输出

{
    "data": {
        "yesterday": {
            "date": "16日星期六",
            "high": "高温 3℃",
            "fx": "北风",
            "low": "低温 -6℃",
            "fl": "<![CDATA[3-4级]]>",
            "type": ""
        },
        "city": "北京",
        "forecast": [{
            "date": "17日星期天",
            "high": "高温 6℃",
            "fengli": "<![CDATA[<3级]]>",
            "low": "低温 -4℃",
            "fengxiang": "北风",
            "type": ""
        }, {
            "date": "18日星期一",
            "high": "高温 4℃",
            "fengli": "<![CDATA[<3级]]>",
            "low": "低温 -3℃",
            "fengxiang": "南风",
            "type": ""
        }, {
            "date": "19日星期二",
            "high": "高温 7℃",
            "fengli": "<![CDATA[<3级]]>",
            "low": "低温 -4℃",
            "fengxiang": "西南风",
            "type": "多云"
        }, {
            "date": "20日星期三",
            "high": "高温 9℃",
            "fengli": "<![CDATA[<3级]]>",
            "low": "低温 -4℃",
            "fengxiang": "北风",
            "type": ""
        }, {
            "date": "21日星期四",
            "high": "高温 11℃",
            "fengli": "<![CDATA[<3级]]>",
            "low": "低温 -3℃",
            "fengxiang": "西南风",
            "type": ""
        }],
        "ganmao": "各项气象条件适宜,无明显降温过程,发生感冒机率较低。",
        "wendu": "-2"
    },
    "status": 1000,
    "desc": "OK"
} < class \'str\' > 

{
\'data\': { \'yesterday\': { \'date\': \'16日星期六\', \'high\': \'高温 3℃\', \'fx\': \'北风\', \'low\': \'低温 -6℃\', \'fl\': \'<![CDATA[3-4级]]>\', \'type\': \'\' }, \'city\': \'北京\', \'forecast\': [{ \'date\': \'17日星期天\', \'high\': \'高温 6℃\', \'fengli\': \'<![CDATA[<3级]]>\', \'low\': \'低温 -4℃\', \'fengxiang\': \'北风\', \'type\': \'\' }, { \'date\': \'18日星期一\', \'high\': \'高温 4℃\', \'fengli\': \'<![CDATA[<3级]]>\', \'low\': \'低温 -3℃\', \'fengxiang\': \'南风\', \'type\': \'\' }, { \'date\': \'19日星期二\', \'high\': \'高温 7℃\', \'fengli\': \'<![CDATA[<3级]]>\', \'low\': \'低温 -4℃\', \'fengxiang\': \'西南风\', \'type\': \'多云\' }, { \'date\': \'20日星期三\', \'high\': \'高温 9℃\', \'fengli\': \'<![CDATA[<3级]]>\', \'low\': \'低温 -4℃\', \'fengxiang\': \'北风\', \'type\': \'\' }, { \'date\': \'21日星期四\', \'high\': \'高温 11℃\', \'fengli\': \'<![CDATA[<3级]]>\', \'low\': \'低温 -3℃\', \'fengxiang\': \'西南风\', \'type\': \'\' }], \'ganmao\': \'各项气象条件适宜,无明显降温过程,发生感冒机率较低。\', \'wendu\': \'-2\' }, \'status\': 1000, \'desc\': \'OK\' } < class \'dict\' >

 

将字典存入文件中,再从文件读取出来

#coding=utf-8
import json
d1 = {\'name\':\'foot\'}

\'\'\'
第一种写法
json.dump(d1,open(\'db\',\'w\'))
d1 = json.load(open(\'db\',\'r\'))
print(d1,type(d1))
\'\'\'

#第二种写法
with open("db","w") as f:
    json.dump(d1,f)

with open("db","r") as f:
    print(json.load(f))

 

参考:

http://www.cnblogs.com/bainianminguo/p/6676067.html

以上是关于python的json模块的dumps,loads,dump,load方法介绍的主要内容,如果未能解决你的问题,请参考以下文章

python的json模块的dumps,loads,dump,load方法介绍

python的json模块的dumps,loads,dump,load方法介绍

python的json模块的dumps,loads,dump,load方法介绍

Python json模块dumps loads

json模块 dumps,dump,loads,load

python中json模块dumps和loads的区分