我已经在堆栈溢出和Google上搜索了有关此错误的详细信息,但找不到解决我的情况的解决方案。
我正在使用棉花糖对我的api get请求进行序列化/反序列化。
@post_user_blueprint.route('/api/v1/get_current_profile', methods=['GET'])
@login_required
def get_profile():
query_user_by_order = User.query.join(Sample) \
.join(Individual) \
.join(Ordering) \
.join(Family) \
.join(Sequence) \
.filter(User.id == Sample.fk_user_id) \
.filter(Individual.id == Sample.fk_individual_id) \
.filter(Sample.id == Sequence.fk_sample_id) \
.filter(Individual.id == Ordering.fk_individual_id) \
.filter(Family.id == Individual.fk_family_id) \
.filter(User.email == current_user.email)\ ***********
.all()
result = profile_users_schema.dump(query_user_by_order)
return jsonify({'user': result.data})
@login_required
@post_user_blueprint.route('/profile', methods=['GET', 'POST'])
def profile():
response = requests.get('http://127.0.0.1:5000/api/v1/get_current_profile')
response = response.text
data = json.loads(response) $$$$$$$$$$$$$$$$$
return render_template('profile.html', user_object = data)
*****:当我省略此行时,此方法工作正常,我添加该行的唯一原因是显示登录其个人资料的人,而不显示所有人的个人资料。
$$$$$:这是错误所在。
完整的追溯
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/genomics/PycharmProjects/sample_accessioning/app/views/post_inputs.py", line 209, in profile
profile = response.json()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/requests/models.py", line 826, in json
return complexjson.loads(self.text, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我不明白为什么会这样,在我将@login_required放在def get_profile上方之后,******行没有直接显示其错误。但是当@login_required不存在时,它会抛出
AttributeError: 'AnonymousUser' object has no attribute 'email'
如果可能的话,我想知道一种获取当前用户电子邮件的方法,这样我就可以查询当前用户的个人资料,而不是整个用户的个人资料。另外,我真的很想知道为什么会出现下面的错误。
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
谢谢,
最佳答案
好的,所以我找到了解决方案,我将全部功劳归功于AArias,因为他建议了一种更干净的方式来获取响应数据。因此,由于HTTP返回的是字节而不是字符串,因此您需要将其显式解码为utf-8,并且许多现有的synthax无法正常工作。.我不知道为什么。
下面的代码有效。
response = get_profile().data
data = json.loads(response.decode('utf-8'))
如果有人尝试使用.read()或.readall()对其进行解码,则它将不起作用,因为响应对象没有read或readall方法。最好的方法是最简单的方法,只需对其进行解码并使用'loads'而不是'load'。
希望它能帮助某人
关于python - Flask Json json.decoder.JSONDecodeError:预期值:第1行第1列(字符0),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41309185/