我目前得到一个很长的 JSON,我正试图通过 Python 2.7 从中挑选出 2 条信息。
JSON 大致如下所示:
{
'device': [
{
'serial': '00000000762c1d3c',
'registered_id': '019'
},
{
'serial': '000000003ad192f2',
'registered_id': '045'
},
{
'serial': '000000004c9898aa',
'registered_id': '027'
}
],
}
在此 JSON 中,我正在寻找可能与 JSON 中的序列匹配的特定序列。如果是这样,它也应该打印出 registered_id。
我试过使用一个简单的脚本,即使没有 registered_id 但我一无所获。:
if '00000000762c1d3c' not in data['device']:
print 'not there'
else:
print 'there'
感谢您的建议!
原文由 user5740843 发布,翻译遵循 CC BY-SA 4.0 许可协议
date['device']
包含一个对象列表,所以你应该这样对待它并迭代它们:这是使用某种鲜为人知的
for-else
构造: https ://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on -循环