四 python josn轉字典字元

2022-08-02 13:36:12 字數 1332 閱讀 2824

josn基本操作

舉例原始碼

#!/usr/bin/python3

# encoding:utf-8

import json

import requests

class jsonc():

def __init__(self):

self.url = '北京'

self.geturl = requests.get(self.url)

#字典轉json,因為python沒json型別所以str表示

def dict_json(self):

d =

j = json.dumps(d,ensure_ascii=false)

print('dict_json函式:型別:',type(d),'轉型別',type(j),'\n',j)

#json轉字典

def json_dict(self):

s = ''

d = json.loads(s)

print('json_dict函式:型別:',type(s),'轉型別',type(d))

#介面呼叫直接返回 字典(dict)

def get_json(self):

d = self.geturl.json()

print('get_json函式型別:',type(d))

#介面呼叫直接返回字串

def get_str(self):

s = self.geturl.text

print('get_str函式返回型別:',type(s))

if __name__=="__main__":

js = jsonc()

js.dict_json()

js.json_dict()

js.get_json()

js.get_str()

執行結果

dict_json函式:型別: 轉型別 

json_dict函式:型別: 轉型別 get_json函式型別: get_str函式返回型別:

呼叫get例子

,

"city":"北京","forecast":

[ ,,,

,],"ganmao":"各項氣象條件適宜,無明顯降溫過程,發生感冒機率較低。","wendu":"29"

},"status":1000,"desc":"ok"

}

《轉》python學習(9)字典

一 對映型別 我理解中的對映型別是 鍵值對的關係,鍵 key 對映值 value 且它們是一對多的關係。字典是python唯一的對映型別。擴充套件1 雜湊表 一種資料結構,值是根據相關的鍵進行資料儲存的,形成 鍵 值對 key value pairs 雜湊表中的值是沒有順序的。擴充套件2 對映型別與...

json 轉字典 字典轉json

brief 把格式化的json格式的字串轉換成字典 param jsonstring json格式的字串 return 返回字典 json格式字串轉字典 nsdictionary dictionarywithjsonstring nsstring jsonstring nsdata jsondata...

Python學習四 字典

在python中,字典是一系列鍵 值對。每個鍵都與乙個值相關聯,你可以使用鍵來訪問與之相關的值。事實上,可將任何python物件用作字典中的值,例如數字 字串 列表 甚至是字典。例項 alien print alien age 輸出17 user 0 for key,value in user 0....