【发布时间】:2020-07-14 06:20:40
【问题描述】:
我的代码有问题,没有出现错误,但由于某种原因,我没有得到想要的结果 结果
这是用户将收到的“json”数据
books = [
{'id': 0,
'title': 'A fire Upon the Deep',
'author': 'Vernor Vinge',
'first_sentence': 'The coldsleep itself was dreamless.',
'year_published': '1992'},
{'id': 1,
'title': 'The Ones Who Walk Away From Omelas',
'author': 'Ursula K. Le Guin',
'first_sentence': 'With a clamor of bells that set the swallows soaring, the \
Festival of Summer came to the city Omelas, bright-towered by the sea.',
'published': '1973'},
{'id': 2,
'title': 'Dhalgren',
'author': 'Samuel R. Delany',
'first_sentence': 'to wound the autumnal city.',
'published': '1975'}
]
如果不存在 id,那么它会返回一条错误消息,因为每本书都有一个 id,所以它不应该看到该消息
@app.route('/api/v1/resource/books', methods=['GET'])
def api_id():
if 'id' in request.args:
id = int(request.args['id'])
else:
return "Error: ID not provided. Please specify an ID"
results = []
for book in books:
if book['id'] == id:
results.append(book)
return jsonify(results)
【问题讨论】:
标签: json python-3.x api dictionary