json和字典之间的转换

 json和字典之间的转换,json是双引号,字典是单引号

# json格式
str1 = '''{
    "aac003" : "marry",
    "tel" : "13449872721",
    "crm003" : "1",
    "crm004" : "1"
}'''

str1_new = json.loads(str1)  # loads()方法,将json格式转为真正的字典
print(type(str1_new))
print(str1_new)
str2 = json.dumps(str1_new)  # dumps()方法,将字典转为json格式
print(type(str2))
print(str2)
# 单引号是字典,双引号是json字符串

输出代码:

"D:\Program Files (x86)\Python\Python310\python.exe" "D:/Program Files/PycharmProjects/Delivery_System/testcases/aaaa.py"

{'aac003': 'tom', 'tel': '13449872721', 'crm003': '1', 'crm004': '1'}

{"aac003": "tom", "tel": "13449872721", "crm003": "1", "crm004": "1"}

Process finished with exit code 0

你可能感兴趣的:(json)