处理Json请求时出错

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

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

这是我在python烧瓶中分析的json:

{"description": "URL: https://sometest.xyz.com/test16/apicalln Issue: sample issuen Result: failn Severity: lown 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)

您正在为_jsonreq分配JSON字符串,但随后尝试将其用作字典。 尝试将您的代码更改为:

@app.route('/', methods=['POST','GET'])
def main():
    _jsonreq = request.json
    return _jsonreq['project']
链接地址: http://www.djcxy.com/p/48563.html

上一篇: Error processing Json request

下一篇: How to PUT a json object with an array using curl