当我在flask反斜杠应用程序中发布内容

2024-09-30 08:28:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用标记“name”和“text”将文本发布到json列表中

@app.route('/', methods=['POST'])
def create_post():

    with open('posts.json') as f:
        posts = json.load(f)
    data = request.data.decode("UTF-8")
    posts.append(data)
    with open('posts.json', 'w') as f:
        f.write(json.dumps(posts))
    return json.dumps(posts)

当我使用它时,我会发布文本,但它看起来像这样

"{\"name\": \"exemple\", \"text\": \"exempel\"}"

但我需要这个

{"name": "exempel", "text": "exempel"}

Tags: textname标记文本jsonapp列表data

热门问题