我想從我的數據庫讀取數據,然後創建並返回一個JSON格式到我的Python Web服務器(web.py)。但下面的代碼不起作用,並給我這個錯誤:Python(json):TypeError:期望的字符串或緩衝區
類型錯誤:預期的字符串或緩衝區
載荷和載荷還轉儲和轉儲(這是我無法理解爲什麼)。正如你明白我很新的Python和JSON格式我會之間切換非常感謝你幫助我(看到很多關於這個問題的帖子,但仍然不知道該怎麼辦)
def ara_json(str):
web.header('Content-Type','application/json; charset=utf-8', unique=True)
cnx = mysql.connector.connect(user='arda', password='1', database='worddb')
cursor = cnx.cursor()
sqlq = "SELECT * FROM names WHERE name = '%s'" %str
cursor.execute(sqlq)
rows = cursor.fetchall()
result=[]
for row in rows:
d = dict()
d['name'] = row[0]
d['type'] = row[1]
result.append(d)
subjects = json.loads(result).read()
return json.dump(subjects , indent=4)
class json_isimbul:
def GET(self,isim):
web.header('Content-Type','application/json; charset=utf-8', unique=True)
isim = isim.lower()
return ara_json(isim)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 239, in process
return self.handle()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 230, in handle
return self._delegate(fn, self.fvars, args)
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 420, in _delegate
return handle_class(cls)
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 396, in handle_class
return tocall(*args)
File "/home/arda/Downloads/arda/tmp/tornado/ps/sa2.py", line 98, in GET
return ara_json(isim)
File "/home/arda/Downloads/arda/tmp/tornado/ps/sa2.py", line 53, in ara_json
subjects = json.loads(result).read()
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
你可以顯示你的異常的追溯?只有類型和簡短信息,很難知道代碼的哪一部分會引發該錯誤,並且回溯會顯示得非常清楚。 – Blckknght
對不起。只是編輯,希望你能理解 –