python字典数据类型

1、vinfo: dict[str, str]

import json

version_json = '''
{
 "date": "2022-05-19T18:09:04-0600",
 "dirty": false,
 "error": null,
 "full-revisionid": "99766d3bd6da8a3fbf3e3e3867cd98e36f4c0b8c",
 "version": "1.22.4"
}
'''  # END VERSION_JSON

def get_versions():
    return json.loads(version_json)
print(get_versions())

{'date': '2022-05-19T18:09:04-0600', 'dirty': False, 'error': None, 'full-revisionid': '99766d3bd6da8a3fbf3e3e3867cd98e36f4c0b8c', 'version': '1.22.4'}

vinfo: dict[str, str]定义vinfo为字典,键和值都是字符串

import json

version_json = '''
{
 "date": "2022-05-19T18:09:04-0600",
 "dirty": false,
 "error": null,
 "full-revisionid": "99766d3bd6da8a3fbf3e3e3867cd98e36f4c0b8c",
 "version": "1.22.4"
}
'''  # END VERSION_JSON

def get_versions():
    return json.loads(version_json)
vinfo: dict[str, str] = get_versions()
print(vinfo)

2、字典的get方法

dict = {'a':1,'b':2}
print(dict.get('a',999))
print(dict.get('c',999))

1

999

你可能感兴趣的:(python,json,字典)