如何仅为id:“ resolution”打印“ value”字符串?
在这种情况下,我要打印“固定”值
customFields: {
string: [
{
id: "device_type",
value: "iPhone 6"
},
{
id: "os_version",
value: "iOS 10.x"
},
{
id: "rabbit_build",
value: "2.11.llyu"
},
{
id: "resolution",
value: "Fixed"
},
我的Python代码是
for ib in data['documents']:
sid = ib['id']
tit = ib['title']
stat = ib['status']
nstep = ib['next_step']['action']
requester = ib['requesterIdentity']
resolution = ib['customFields']['string']
print(sid, tit, stat, nstep, requester, resolution)
输出显示所有id和值,而我只想显示id:“ resolution”的“ value”
答案 0 :(得分:0)
这非常简单,但应该可以:
for ib in data['documents']:
sid = ib['id']
tit = ib['title']
stat = ib['status']
nstep = ib['next_step']['action']
requester = ib['requesterIdentity']
for item in ib['customFields']['string']:
if item['id'] == "resolution":
resolution = item['string']
当然,由于“字符串”是字典的列表,因此,如果不遍历该列表的所有字典,就无法获得特定的值。我还建议您将此列表更改为字典,这样您的json结构将如下所示:
string: {
"device_type": iPhone 6".
"os_version": "iOS 10.x",
"rabbit_build": "2.11.llyu",
"resolution": "Fixed",
}
通过这种结构,您可以通过直接获取“分辨率”键的值来检查所有值而无循环
答案 1 :(得分:0)
您当前的代码没有执行您想要的操作,因为您将整个列表存储在ib ['customField'] ['string']中并打印此新变量的值,该变量称为“ resolution”。 (“ ib”是什么?)
此新变量的名称与'='运算符的行为之间没有任何关系。
您正在尝试获取列表中的特定元素,请检查'id'是否包含'resolution'然后打印出来。
您可以使用
resolutionList = [it for it in ib['customFields']['string'] if it['id'] == 'resolution']
for element in resolutionList :
print(element['value'])
或(一行)
print(*[it['value'] for it in ib['customFields']['string'] if it['id'] == 'resolution'])
*运算符用于删除列表(print(* [a,b])<=> print(a,b))