Summary of the Method of Conversion between python Dictionary and json Dictionary

  • 2021-09-05 00:26:30
  • OfStack

In python, json is composed of list and dictionary respectively. This paper mainly introduces the conversion method between dictionary and json in python. Use json. dumps to convert a dictionary into an json string. Use json. loads to convert an json string to dictionary type data.

1. Dictionary to json

Use json. dumps

json. dumps encodes an python object into an json object, and can convert a dictionary into an json string.

Method format


# Dictionary conversion to json String  
json.dumps(dict)

Instances


#  Create a dictionary 
info_dict = {'name': 'Joe', 'age': 20, 'job': 'driver'}
# dumps  Convert data to strings 
info_json = json.dumps(info_dict,sort_keys=False, indent=4, separators=(',', ': '))
#  Display data type 
print(type(info_json))
f = open('info.json', 'w')
f.write(info_json)

2. json to Dictionary

Use json. loads

json. loads decodes an json object into an python object, which is used to convert dictionary-type data into an json string.

Method format


#json String to dictionary 
json.loads(json_str)

Use instances


In [25]: j 
Out[25]: '{"name": "mary", "age": 21}' 
In [26]: result = json.loads(j) 
In [27]: result 
Out[27]: {'name': 'mary', 'age': 21} 
In [28]: type(result) 
Out[28]: dict

Extension of python Dictionary and json String Conversion


import json
"""
dumps: Will python Converts dictionaries in to strings 
output:
{'fontFamily': ' Microsoft Yahei ', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}}
{"fontFamily": "\u5fae\u8f6f\u96c5\u9ed1", "fontSize": 12, "BaseSettings": {"font": 1, "size": {"length": 40, "wigth": 30}}}
"""
def json_dumps():
json_dict = {'fontFamily': ' Microsoft Yahei ', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}}
print(type(json_dict))
print(json_dict)
json_str = json.dumps(json_dict)
print(type(json_str))
print(json_str)
"""
dump: Write data to json In a file 
"""
def json_dump():
json_dict = {'fontFamily': ' Microsoft Yahei ', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}}
with open("../file/record.json", "w")as f:
json.dump(json_dict, f)
print("finished")
"""
loads: Convert a string to a dictionary 
output:
{"fontFamily": " Microsoft Yahei ", "fontSize": 12, "BaseSettings": {"font": 1, "size": {"length": 40, "wigth": 30}}}
{'fontFamily': ' Microsoft Yahei ', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}}
"""
def json_loads():
json_str = '{"fontFamily": "\u5fae\u8f6f\u96c5\u9ed1", "fontSize": 12, "BaseSettings": {"font": 1, "size": {"length": 40, "wigth": 30}}}'
print(type(json_str))
print(json_str)
json_dict = json.loads(json_str)
print(type(json_dict))
print(json_dict)
"""
load: Read the file and transform the string into Python Data type 
output:
40
{'fontFamily': ' Microsoft Yahei ', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}}
"""
def json_load():
f = open("../file/record.json", encoding='utf-8')
setting = json.load(f)
print(setting['BaseSettings']['size']['length'])
setting['BaseSettings']['size']['length'] = 40
print(setting)
if __name__ == '__main__':
json_dumps()
json_dump()
json_loads()
json_load()

Related articles: