Python, Pyodbc - 将行编码为JSON
class GetAdminAsJsonHandler(BaseHandler):
def post(self):
admin_guid = self.get_argument("admin_guid")
if admin_guid == None:
self.write("AdminGuid is missing.")
else:
database = self.connect_db()
cursor = database.cursor()
admins = cursor.execute("SELECT * FROM Admin").fetchall()
self.disconnect_db()
self.write(json_encode(admins)) # Error
[E 141021 02:22:47 web:1407] Uncaught exception POST /admin/get (::1)
HTTPServerRequest(protocol='http', host='localhost:8000', method='POST', uri='/admin/get', version='HTTP/1.1', remote_ip='::1', headers={'Origin': 'http://localhost:8000', 'Content-Length': '12', 'Accept-Language': 'ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4', 'Accept-Encoding': 'gzip,deflate', 'Host': 'localhost:8000', 'Accept': '*/*', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36', 'Connection': 'keep-alive', 'X-Requested-With': 'XMLHttpRequest', 'Referer': 'http://localhost:8000/admin', 'Cookie': 'csrftoken=NUV1oVLBJ3jzzvuNio9Dv22k8qTt5TYW; install_421aa90e079fa326b6494f812ad13e79=bqgjn14u6t5sn3th0ccdnj6pr7', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'})
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\tornado\web.py", line 1332, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "C:\Users\SANG MIN\Desktop\haundae\module\admin\handlers.py", line 34, in post
admin_array = [a for r in admin] = {}
File "C:\Python27\lib\site-packages\tornado\escape.py", line 88, in json_encode
return json.dumps(value).replace("</", "<\\/")
File "C:\Python27\lib\json\__init__.py", line 243, in dumps
return _default_encoder.encode(obj)
File "C:\Python27\lib\json\encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Python27\lib\json\encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "C:\Python27\lib\json\encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: ('admin', 'admin', '\xc3\xd6\xb0\xed\xbf\xee\xbf\xb5\xc1\xf8', True, True, 1) is not JSON serializable
[E 141021 02:22:47 web:1811] 500 POST /admin/get (::1) 1233.00ms
你好,我需要开发一个处理程序,当网页通过AJAX请求时,它能返回数据库中管理员的JSON编码列表。使用Pyodbc,并假设表的列名是AdminGuid、ID和Name,我应该如何修正那个#Error部分才能让它正常工作呢?
谢谢!!
*错误信息
1 个回答
1
这是我想要的内容。希望能帮助到其他遇到同样问题的人。