Python (json) : TypeError: expected string or buffer
Python (json) : TypeError: expected string or buffer
我想从我的数据库中读取数据,然后创建 return 一个 json 格式到我的 python 网络服务器 (web.py)。但是下面的代码没有不工作并给我这个错误:
类型错误:预期的字符串或缓冲区
在负载和负载之间切换也转储和转储(我不明白为什么)。如您所知,我对 python 和 json 格式都很陌生,如果你帮帮我(看到很多关于这个问题的 post 但仍然不知道该怎么做)
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
json.loads()
需要一个字符串。正确的方法是首先构建数据并使用 json.dumps
,这将为您提供一个 json 对象(实际上是一个字符串)。希望这有帮助。
你必须改两行就可以了。
- 删除这一行:subjects = json.loads(result).read()
- 然后这样做:return json.dumps(result , indent=4)
希望对您有所帮助。 :)
我想从我的数据库中读取数据,然后创建 return 一个 json 格式到我的 python 网络服务器 (web.py)。但是下面的代码没有不工作并给我这个错误:
类型错误:预期的字符串或缓冲区
在负载和负载之间切换也转储和转储(我不明白为什么)。如您所知,我对 python 和 json 格式都很陌生,如果你帮帮我(看到很多关于这个问题的 post 但仍然不知道该怎么做)
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
json.loads()
需要一个字符串。正确的方法是首先构建数据并使用 json.dumps
,这将为您提供一个 json 对象(实际上是一个字符串)。希望这有帮助。
你必须改两行就可以了。
- 删除这一行:subjects = json.loads(result).read()
- 然后这样做:return json.dumps(result , indent=4)
希望对您有所帮助。 :)