我尝试使用python构建HTTP-API。我有一个JSON格式的对象数组。我想读取其中一个对象的值。
在我的python脚本中,我将数据库表附加到对象数组。我正在寻找一种解决方案来选择其中一个对象中的单个值。
我有一个功能:
cur.execute()
row_headers=[x[0] for x in cur.description]
response = cur.fetchall()
json_data=[]
for result in response:
json_data.append(dict(zip(row_headers,result)))
return jsonify(json_data)
回报看起来像:
"ID": 123,
"CODE": 4117,
"STATUS": "off",
"ID": 345,
"CODE": 5776,
"STATUS": "on",
]
我正在寻找一个函数(inputID):
where ID = inputID
set currentcode =
set currentstatus =
def return_with_id(input_id):
for x in json_data:
if x['ID'] == input_id:
return x
这将循环遍历每个索引json_data并检查该索引的ID是否等于您要求的内容,如果找到匹配的内容,它将返回该内容并且该函数完成。
如果你想用它来做其他的事情,你可以通过在代码返回之前编辑代码来实现。
2019-07-17 23:26:20