提问者:小点点

处理Json请求时出错


我正在发送一个json请求,并在python flask服务中捕获请求参数以进一步处理。但是,当我json转储请求并试图从中收集特定的参数值时,它会给我带来错误

“字符串索引必须是整数,而不是字符串”

这是我在python flask中解析的json:

{"description": "URL: https://sometest.xyz.com/test16/apicall\\n Issue: sample issue\\n Result: fail\\n Severity: low\\n Category: sample category", "issuetype": {"name": "Bug"}, "priority": {"name": "Trivial"}, "project": "sample-project", "summary": "sample issue - https://sometest.xyz.com/test16/apicall"}

我不知道我做错了什么。

下面是我的python烧瓶代码:

    from flask import Flask
    from flask import request
    from flask import json
    import httplib
    httplib.HTTPConnection.debuglevel = 2

    app = Flask(__name__)

    @app.route('/', methods=['POST','GET'])
    def main():
        _jsonreq = json.dumps(request.json)
        return _jsonreq['project']

    if __name__ == "__main__":
        app.run(debug=True)

共1个答案

匿名用户

您将一个JSON字符串分配给_jsonreq,但然后尝试像使用字典一样使用它。尝试将代码更改为:

@app.route('/', methods=['POST','GET'])
def main():
    _jsonreq = request.json
    return _jsonreq['project']