Py学习  »  Python

为api请求在python中jsonify制表符

Bagel • 4 年前 • 674 次点击  

在那里,我从mysql得到了一个标签如下: {'jean': 22, 'pierre': 26, 'john': 21, 'charles': 23, 'test': 25, 'test2': 45} 被叫用户

我想这样做:

[{
  "name": "jean"
  "age": 22
}
{
  "name": "pierre"
  "age" : 26
}
...

但我做不到。你能帮帮我吗?

@app.route("/users", methods = ['GET'])
def getUsers():
    return jsonify({'users' : users}] 
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40261
 
674 次点击  
文章 [ 1 ]  |  最新文章 4 年前
grapes
Reply   •   1 楼
grapes    5 年前

它将如下所示:

d = {'jean': 22, 'pierre': 26, 'john': 21, 'charles': 23, 'test': 25, 'test2': 45}
res = []
for k, v in d.items():
  res.append({"name": k, "age": v})

print(res)