python開發筆記-str轉字典


后台接收到post請求數據格式為json格式的字符串,不能直接用字典的get方法

909090909090909090909090909090909
Internal Server Error: /get_data/sql_server/
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/get_data_from_sqlserver/get_data/views.py", line 45, in get_data_from_sql_server
    request_token = request_data.get('token')
AttributeError: 'str' object has no attribute 'get'

json dict格式的字符串,轉字典,可以用eval方法

例:

>>> test = "{'name' : 'jesson', 'sex' : 'male', 'age': 28}"
>>> test01=eval(test)
>>> type(test)
<class 'str'>
>>> 
>>> type(test01)
<class 'dict'>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM