我正在使用python 2.7 \ Linux
我有api结果(见下文):
{"id":"137","iv":0,
"components":[{"id":"928","type":"IDUGW","creation_date":"2016-05-04 08:10:38.0","update_date":"2016-05-04 08:10:38.0","unit_id":"137","serial_number":"00000000501C8349"},
{"id":"927","type":"IDU","creation_date":"2016-05-04 08:10:37.0","update_date":"2016-05-04 08:10:37.0","unit_id":"137","serial_number":"00000000501C8268"},
{"id":"930","type":"Battery","creation_date":"2016-05-04 08:10:40.0","update_date":"2016-05-04 08:10:40.0","unit_id":"137","serial_number":"00000000501C802A"}
,{"id":"929","type":"Panel","creation_date":"2016-05-04 08:10:39.0","update_date":"2016-05-04 08:10:39.0","unit_id":"137","serial_number":"00000000501C810B"}],
"creation_date":"2016-05-04 08:10:41.0",
"update_date":"2016-05-04 08:10:41.0",
"serial_number":"0011",
"phone_number":"972528745028",
"owner_phone":"9720545555554"}
如果我理解正确,我在字典里面有字典(第二行“组件”是另一个字典,里面有4个键\值)
我正在尝试检查字典中的“type”是否等于我给的参数。
代码:
if(MyDictionary [u'components'] [0] [u'type']<> lRestParams ['type4']):
我有4个索引(0,1,2,3)
有时会传递“if”,有时会更改索引(重建所有内容时)并且失败
我怎么能比较type4和MyDictionary [u'components'] [索引之一] [u'type'] 另外我需要在那之后比较key = value4的值 所以这意味着如果我们检查索引x而不是键,那么应该在那里检查。
希望不要复杂
提前致谢
辖
答案 0 :(得分:0)
您可以遍历组件值来进行比较,而不是给出索引号。
for keys in data['components']:
if keys['type'] == "IDUGW":
print "matched"