Python json轉(zhuǎn)字典字符方法實(shí)例解析
josn基本操作
1.導(dǎo)入import json
2.字典轉(zhuǎn)json:json.dumps(dict,ensure_ascii=False),加,ensure_ascii=False轉(zhuǎn)換之后無中文亂碼
3.json轉(zhuǎn)字典:json.loads(str)
4.json轉(zhuǎn)字典:requests.get().josn()
5.返回字符串: requests.get().text
舉例源碼
#!/usr/bin/python3
# encoding:utf-8
import json
import requests
class jsonC():
def __init__(self):
self.url = 'http://wthrcdn.etouch.cn/weather_mini?city=北京'
self.geturl = requests.get(self.url)
#字典轉(zhuǎn)json,因?yàn)閜ython沒json類型所以str表示
def dict_json(self):
d = {"name":"張三","age":18}
j = json.dumps(d,ensure_ascii=False)
print('dict_json函數(shù):類型:',type(d),'轉(zhuǎn)類型',type(j),'\n',j)
#json轉(zhuǎn)字典
def json_dict(self):
s = '{"name":"張三","age":18}'
d = json.loads(s)
print('json_dict函數(shù):類型:',type(s),'轉(zhuǎn)類型',type(d))
#接口調(diào)用直接返回 字典(dict)
def get_json(self):
d = self.geturl.json()
print('get_json函數(shù)類型:',type(d))
#接口調(diào)用直接返回字符串
def get_str(self):
s = self.geturl.text
print('get_str函數(shù)返回類型:',type(s))
if __name__=="__main__":
js = jsonC()
js.dict_json()
js.json_dict()
js.get_json()
js.get_str()
運(yùn)行結(jié)果
dict_json函數(shù):類型: <class 'dict'> 轉(zhuǎn)類型 <class 'str'>
{"name": "張三", "age": 18}
json_dict函數(shù):類型: <class 'str'> 轉(zhuǎn)類型 <class 'dict'>
get_json函數(shù)類型: <class 'dict'>
get_str函數(shù)返回類型: <class 'str'>
調(diào)用get例子
http://wthrcdn.etouch.cn/weather_mini?city=北京
返回json值:
{"data":
{"yesterday":
{"date":"28日星期六","high":"高溫 30℃","fx":"西南風(fēng)","low":"低溫 17℃","fl":"<![CDATA[<3級]]>","type":"晴"},
"city":"北京","forecast":
[
{"date":"29日星期天","high":"高溫 29℃","fengli":"<![CDATA[<3級]]>","low":"低溫 18℃","fengxiang":"南風(fēng)","type":"晴"},
{"date":"30日星期一","high":"高溫 28℃","fengli":"<![CDATA[<3級]]>","low":"低溫 19℃","fengxiang":"南風(fēng)","type":"晴"},
{"date":"1日星期二","high":"高溫 29℃","fengli":"<![CDATA[<3級]]>","low":"低溫 20℃","fengxiang":"南風(fēng)","type":"多云"},
{"date":"2日星期三","high":"高溫 29℃","fengli":"<![CDATA[<3級]]>","low":"低溫 17℃","fengxiang":"南風(fēng)","type":"晴"},
{"date":"3日星期四","high":"高溫 30℃","fengli":"<![CDATA[<3級]]>","low":"低溫 12℃","fengxiang":"東南風(fēng)","type":"多云"}
],"ganmao":"各項(xiàng)氣象條件適宜,無明顯降溫過程,發(fā)生感冒機(jī)率較低。","wendu":"29"
},"status":1000,"desc":"OK"
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python將字典內(nèi)容寫入json文件的實(shí)例代碼
- Python xml、字典、json、類四種數(shù)據(jù)類型如何實(shí)現(xiàn)互相轉(zhuǎn)換
- Python json讀寫方式和字典相互轉(zhuǎn)化
- python字典和json.dumps()的遇到的坑分析
- python通過對字典的排序,對json字段進(jìn)行排序的實(shí)例
- Python 將json序列化后的字符串轉(zhuǎn)換成字典(推薦)
- Python中字典和JSON互轉(zhuǎn)操作實(shí)例
- python中將字典轉(zhuǎn)換成其json字符串
- python字典與json轉(zhuǎn)換的方法總結(jié)
相關(guān)文章
Python3如何根據(jù)函數(shù)名動態(tài)調(diào)用函數(shù)
這篇文章主要介紹了Python3如何根據(jù)函數(shù)名動態(tài)調(diào)用函數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Python 注解方式實(shí)現(xiàn)緩存數(shù)據(jù)詳解
這篇文章主要介紹了Python 注解方式實(shí)現(xiàn)緩存數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-10-10
用python實(shí)現(xiàn)前向分詞最大匹配算法的示例代碼
這篇文章主要介紹了用python實(shí)現(xiàn)前向分詞最大匹配算法的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

