Conversion between python dictionary and json

Dictionary and json conversion mainly use the following methods:

  • loads(): convert json data into dict data
  • dumps(): convert dict data into json data

Dictionary to json data:

In [5]: import json                                                                                                                                                                                                                                                           

In [6]: dict = {'name':'mary','age':21}                                                                                                                                                                                                                                       

In [7]: type(dict)                                                                                                                                                                                                                                                            
Out[7]: dict

In [8]: j = json.dumps(dict)                                                                                                                                                                                                                                                  

In [9]: j                                                                                                                                                                                                                                                                     
Out[9]: '{"name": "mary", "age": 21}'

It should be noted here that if the dictionary contains Chinese characters when converting to json, encoding problems will occur , as follows:

In [29]: staff = {'name':'权权','age':23,'sex':'女'}                                                                                                                                                                                                                          

In [30]: json.dumps(staff)                                                                                                                                                                                                                                                    
Out[30]: '{"name": "\\u6743\\u6743", "age": 23, "sex": "\\u5973"}'

So if there is Chinese, we need to add parameters for processing:

In [31]: json.dumps(staff,ensure_ascii=False)                                                                                                                                                                                                                                 
Out[31]: '{"name": "权权", "age": 23, "sex": "女"}'

原因: Usually the request is json data in post mode, but if there is Chinese, there is a problem, because Chinese is encoded in unicode, but it is parsed in ASCII by default. Chinese is not in ASCII encoding, so Chinese cannot be displayed.

Convert json data to dictionary:

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

Guess you like

Origin blog.csdn.net/quanqxj/article/details/89226160