如何使用python从JSON中获取某个值

woobm2wo  于 2023-03-20  发布在  Python
关注(0)|答案(3)|浏览(162)

我正在尝试查看如何从这个json脚本中获取所有者联系方式。

"images": [
      {
         "imageId": "123456",
         "labels": [
            {
               "labelKey": "BA",
               "labelValue": "abc"
            },
            {
               "labelKey": "def",
               "labelValue": "htts://example.com"
            },
            {
               "labelKey": "build number",
               "labelValue": "28"
            },
            {
               "labelKey": "title",
               "labelValue": "title example"
            },
            {
               "labelKey": "owner contact",
               "labelValue": "jon doe"
            }
         ]

我是这么做的:

for i in owner_contact["images"]:
        owner = i["images"][0]
        label_key = owner["labelKey"]["labelValue"]
        print(label_key)

我没有得到所需的结果。有什么建议吗?

jdgnovmf

jdgnovmf1#

试试这个:

my_object =  {   "images": [
      {
         "imageId": "123456",
         "labels": [
            {
               "labelKey": "BA",
               "labelValue": "abc"
            },
            {
               "labelKey": "def",
               "labelValue": "htts://example.com"
            },
            {
               "labelKey": "build number",
               "labelValue": "28"
            },
            {
               "labelKey": "title",
               "labelValue": "title example"
            },
            {
               "labelKey": "owner contact",
               "labelValue": "jon doe"
            }
         ]
} 

def get_owner_contact( image_data):  
   owner_contact = None  
   
   for label in image_data["labels"]:  
      if label["labelKey"] == "owner contact"  
         owner_contact = label["labelValue"]  
   return owner_contact  

for image_data in my_object["images"]:  
   owner_contact = get_owner_contact(image_data)  
   print(owner_contact)
tvmytwxo

tvmytwxo2#

要从给定的JSON中提取“所有者联系人”,应该迭代“images”列表中的“labels”列表,并检查值为“所有者联系人”的“labelKey”。
这段代码将打印给定JSON中的“所有者联系人”值(“jondoe”)。

json_data = {
    "images": [
        {
            "imageId": "123456",
            "labels": [
                {"labelKey": "BA", "labelValue": "abc"},
                {"labelKey": "def", "labelValue": "htts://example.com"},
                {"labelKey": "build number", "labelValue": "28"},
                {"labelKey": "title", "labelValue": "title example"},
                {"labelKey": "owner contact", "labelValue": "jon doe"},
            ],
        }
    ]
}

for image in json_data["images"]:
    for label in image["labels"]:
        if label["labelKey"] == "owner contact":
            owner_contact = label["labelValue"]
            print(owner_contact)

在这里试试:https://www.pythonmorsels.com/p/33sr8/

bkhjykvo

bkhjykvo3#

我假设你的json数据是这样的:

owner_contact = {
    "images": [
        {
            "imageId": "123456",
            "labels": [
                {
                    "labelKey": "BA",
                    "labelValue": "abc"
                },
                {
                    "labelKey": "def",
                    "labelValue": "htts://example.com"
                },
                {
                    "labelKey": "build number",
                    "labelValue": "28"
                },
                {
                    "labelKey": "title",
                    "labelValue": "title example"
                },
                {
                    "labelKey": "owner contact",
                    "labelValue": "jon doe"
                }
            ]
        }
    ]
}

如果你想要一个简单的answer,就像这样循环:

for i in owner_contact["images"]:
    owner = i["labels"]
    for j in owner:
        if j["labelKey"] == "owner contact":
            print(j["labelValue"])

它将打印所有的所有者联系人:

jon doe

相关问题