Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python字典与JSON字符串如何互转 #48

Open
Sogrey opened this issue Feb 16, 2020 · 0 comments
Open

Python字典与JSON字符串如何互转 #48

Sogrey opened this issue Feb 16, 2020 · 0 comments

Comments

@Sogrey
Copy link
Owner

Sogrey commented Feb 16, 2020

d = {'a':123,'b':'456','c':'xyz'}
print(d) ## {'a': 123, 'b': '456', 'c': 'xyz'}
print(type(d))  ## <class 'dict'>
import json
json_str = json.dumps(d)  ##  json.dumps  字典->json
print(json_str)  ## {"a": 123, "b": "456", "c": "xyz"}
print(type(json_str))  ## <class 'str'>

d1 = json.loads(json_str) ## json.loads json->字典
print(d1)  ## {'a': 123, 'b': '456', 'c': 'xyz'}
print(type(d1))  ## <class 'dict'>

使用dumps函数可以将字典转换为JSON字符串,使用loads函数可以将JSON字符串转换为字典。

@Sogrey Sogrey added this to 列表、元祖、字典 in Python QAs Feb 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Python QAs
列表、元祖、字典
Development

No branches or pull requests

1 participant