比较JSON文件中列表中的元素。

huangapple go评论91阅读模式
英文:

compare elements from list in a json file

问题

我有一个情景,我正在尝试将列表元素与 JSON 文件进行比较,如果有匹配项,然后返回特定值并创建 JSON 响应。
以下是我的 JSON 数据:

[
  {
    "evi": 1223,
    "evn": "testapp1",
    "conf": {
      "c_gr": "tot",
      "c_id": "112"
    }
  },
  {
    "evi": 6759,
    "evn": "testapp2",
    "conf": {
      "c_gr": "tot",
      "c_id": "112"
    }
  },
  {
    "evi": 3352,
    "evn": "testapp3",
    "conf": {
      "c_gr": "tot7",
      "c_id": "112"
    }
  }
]

这是我到目前为止尝试过的内容:

response = requests.post('https://testaapp.com', headers=headers, data=data)
resp_json = response.json()
if response.status_code == 200:
    print('working fine...!!')
else:
    print('not working !!')
metadata = resp_json['data']
m_list = [1123, 123445, 61887, 3352, 35811488976]
final_lst_data1 = []
final_lst_data2 = []
for key in m_list:
    temp1 = key
    for keya in metadata:
        if temp1 in metadata[keya]['evi']:
            final_lst_data1.append(metadata['evn'])  # 返回名称
            final_lst_data2.append({'evn': metadata['evn'], 'conf': metadata['conf']})  # 在检查后返回 evn 和 conf 值,然后将其从列表转换为相同的 JSON 格式,如果不可能,不确定如何在此处从列表转换为 JSON。

但这不起作用,因为它给我以下错误:

if key in metadata[keya]['evi']:
TypeError: list indices must be integers or slices, not dict

示例链接

英文:

I have a scenario where I am trying to compare list elements with a json file and if there is a match then return certain values and create a json response.
here are my json data

[
 {
    "evi": 1223,
    "evn": "testapp1",
    "conf": {
        "c_gr": "tot",
        "c_id": "112"
    }
 },
 {
    "evi": 6759,
    "evn": "testapp2",
    "conf": {
        "c_gr": "tot",
        "c_id": "112"
    }
 },
  {
    "evi": 3352,
    "evn": "testapp3",
    "conf": {
        "c_gr": "tot7",
        "c_id": "112"
    }
 }
]

Here is what I have tried so far :

response=requests.post('https://testaapp.com', headers=headers, data=data)
resp_json=response.json()
if response.status_code == 200:
    print ('working fine...!!')
else:
    print ('notworking !!')
metadata=resp_json['data']
m_list=[1123, 123445, 61887, 3352, 35811488976]
final_lst_data1 = []
final_lst_data2 = []
for key in m_list:
    temp1= key
	for keya in metadata:
		if temp1 in metadata[keya]['evi']:
           final_lst_data1.append(metadata['evn']) #return the names
           final_lst_data2.append(metadata['evn'], metadata['conf']) #return evn and conf values after checking and then dump it to json from list in same json format if not possible then convert to json from list, not sure how to do this here.

But this doesnt works as it gives me below error

    if key in metadata[keya]['evi']:
TypeError: list indices must be integers or slices, not dict

答案1

得分: 1

你正在使用字典作为索引。当你说"for keya in metadata:"时,keya是一个指向"metadata"内部字典列表的字典。所以,你不需要使用metadata[keya]来访问每个元素,你可以直接使用'keya'。

for keya in metadata:
    if temp1 == keya['evi']:
       final_lst_data1.append(keya['evn'])
       final_lst_data2.append([keya['evn'], keya['conf']])
英文:

You are using a dictionary as an index. When you say "for keya in metadata:", keya is a dictionary that refers to the list of dictionaries inside 'metadata'. So, you don't need to use metadata[keya] to access each element, you can just use 'keya'.

for keya in metadata:
    if temp1 == keya['evi']:
       final_lst_data1.append(keya['evn'])
       final_lst_data2.append([keya['evn'], keya['conf']])

huangapple
  • 本文由 发表于 2020年1月6日 18:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定