Python(json):TypeError:预期的字符串或缓冲区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36262753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python (json) : TypeError: expected string or buffer
提问by Arda Nalbant
I want to read data from my database then create and return a json format to my python web server (web.py).But following code doesn't work and gives me this error:
我想从我的数据库中读取数据,然后创建一个 json 格式并将其返回到我的 python 网络服务器(web.py)。但是下面的代码不起作用并给了我这个错误:
TypeError: expected string or buffer
类型错误:预期的字符串或缓冲区
Switched between loads and load also dumps and dump (Which i cant understand why).As you understand i'm very new to both python and json format i will be very thankful if you help me out(saw many post about this problem but still cant figure out what to do)
在负载和负载之间切换也会转储和转储(我不明白为什么)。正如你所知,我对 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
回答by Bhimsen
json.loads()
expects a string. The right way to do this is to first construct your data and use json.dumps
which will give you a json object(a string actually). Hope this helps.
json.loads()
期待一个字符串。正确的方法是首先构造你的数据并使用json.dumps
它会给你一个 json 对象(实际上是一个字符串)。希望这可以帮助。
回答by Naresh Chaudhary
You have to change two lines and it will work fine.
您必须更改两行,它会正常工作。
- remove this line: subjects = json.loads(result).read()
- and do like this : return json.dumps(result , indent=4)
- 删除这一行:subjects = json.loads(result).read()
- 并这样做:返回 json.dumps(result , indent=4)
Hopefully, it will help you. :)
希望它会帮助你。:)