我已经彻底搜索堆栈溢出和谷歌有关此错误,我无法找到解决方案来解决我的情况。烧瓶JSON json.decoder.JSONDecodeError:期望值:第1行第1列(char 0)
我正在使用棉花糖序列化/反序列化我的API获取请求。
@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正上方高清get_profile后的错误。但是,当@login_required是不存在,它被扔
AttributeError: 'AnonymousUser' object has no attribute 'email'
如果可能的话,我想知道获得当前用户的电子邮件,所以我可以查询当前用户的配置文件,而不是整体的一种方式。另外,我真的很想知道为什么会出现以下错误。
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
感谢,
很高兴你找到答案。实际上,有一种更好的方式可以在不使用响应的'decode'方法的情况下以unicode字符串的形式获取响应。只需使用'get_data()'方法即可。数据属性实际上最终会被弃用,如下所述:http://flask.pocoo.org/docs/0.12/api/#flask.Response。数据对不起,我的坏。你应该把'.data'改成'.get_data(as_text = True)',然后你应该可以避免使用'.decode'('utf-8')'。我更新了我的答案以反映这一点,请让我知道它是否有效! – AArias
为什么在我9个月后的答案仍然有效时不接受,并且实际上解决了您的问题?不是很酷,因为我花时间来帮助你。 – AArias