【问题标题】:finding a specific object within duplicate element names, python with json在重复的元素名称中查找特定对象,python 和 json
【发布时间】:2022-12-01 00:00:37
【问题描述】:

我想从 objectAttributeValues 中获取 displayValue,其中 objectTypeAttributeId = 14

有多个这样的数组,objectTypeAttributeId = 14 的位置并不总是相同的。如何遍历每个数组以获得特定的 displayValue?

示例 json:

{
  "objectEntries": [{
      "attributes": [{
          "id": "5210",
          "objectAttributeValues": [{
            "displayValue": "10/Nov/22 3:33 PM",
            "referencedType": false,
            "searchValue": "2022-11-10T15:33:49.298Z",
            "value": "2022-11-10T15:33:49.298Z"
          }],
          "objectId": "1201",
          "objectTypeAttributeId": "12"
        },
        {
          "id": "5213",
          "objectAttributeValues": [{
            "displayValue": "02f9ed75-b416-49d0-8515-0601581158e5",
            "referencedType": false,
            "searchValue": "02f9ed75-b416-49d0-8515-0601581158e5",
            "value": "02f9ed75-b416-49d0-8515-0601581158e5"
          }],
          "objectId": "1201",
          "objectTypeAttributeId": "14"
        },
        {
          "id": "5212",
          "objectAttributeValues": [{
            "displayValue": "",
            "referencedType": false,
            "searchValue": "",
            "value": ""
          }],
          "objectId": "1201",
          "objectTypeAttributeId": "11"
        }
      ]
    },
    {
      "attributes": [{
          "id": "4263",
          "objectAttributeValues": [{
            "displayValue": "427904c5-e2c8-4735-bc38-4013928cd043",
            "referencedType": false,
            "searchValue": "427904c5-e2c8-4735-bc38-4013928cd043",
            "value": "427904c5-e2c8-4735-bc38-4013928cd043"
          }],
          "objectId": "1011",
          "objectTypeAttributeId": "14"
        },
        {
          "id": "4262",
          "objectAttributeValues": [{
            "displayValue": "",
            "referencedType": false,
            "searchValue": "",
            "value": ""
          }],
          "objectId": "1011",
          "objectTypeAttributeId": "11"
        }
      ]
    }
  ]
}

对于此示例查询,值将是:

  • 02f9ed75-b416-49d0-8515-0601581158e5
  • 427904c5-e2c8-4735-bc38-4013928cd043

到目前为止,这是我的代码:

from jira import JIRA
import requests
import json

base_url = "url"
auth = basic_auth=('user', 'pass')

headers = {
  "Accept": "application/json"
}

pages = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


for page in pages:
    response = requests.request("GET",base_url + '?page=' + str(page),headers=headers,auth=auth)
    all_output = json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))
    output_dict = json.loads(response.text)
    output_list = output_dict["objectEntries"]
    for outputs in output_list:
        print(outputs["attributes"][0]["objectId"])
        print(outputs["name"])
        print(outputs["objectKey"])
        print(outputs["attributes"][0]["objectAttributeValues"][0]["displayValue"])
        print('\n')

老实说,我什至不知道从哪里开始,所以我还没有尝试太多。任何建议,将不胜感激!!

【问题讨论】:

    标签: python json


    【解决方案1】:

    你可以浏览你的 JSON 字典并继续每个条目,直到你得到你感兴趣的条目。

    # lets browse top level entries of your array
    for e1 in outputs["objectEntries"]:
        # for each of those entries, browse the entries in the attribute section
        for e2 in e1["attributes"]:
            # does the entry match the rule "14"? If not, go to the next one
            if (e2["objectTypeAttributeId"] != 14):
                continue
            # print the current entry's associated value
            for attr in e2["objectAttributeValues"]
                print(attr["displayValue"])
    

    【讨论】:

    • 这似乎不是递归层次结构,不需要递归搜索。
    • @Barmar 是的,更新了一个更简单的解决方案的答案。谢谢 :)
    【解决方案2】:

    如果结构没有改变那么这就是解决方案它将遍历所有对象并在search_values列表中添加displayValue

    display_values = []
    for object_entries in output_dict.get("objectEntries", []):
        for attribute in object_entries.get("attributes"):
            if attribute.get("objectTypeAttributeId") == "14":
                for object_attr in attribute.get("objectAttributeValues", []):
                    if object_attr.get("displayValue") not in display_values:
                        display_values.append(object_attr.get("displayValue"))
    
    
    print(display_values)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 2014-07-18
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      相关资源
      最近更新 更多