猿问

检查json元素或对象是否存在,然后继续

im我正在尝试解析json数据,并在每次元素时收到此错误


 if ['fields']['assignee'] in each:

 TypeError: list indices must be integers or slices, not str

 >>> 

我的 json 是这个


{

"expand": "schema,names",

"startAt": 1,

"maxResults": 50,

"total": 7363,

"issues": [

    {

        "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",

        "id": "591838",

        "self": "https://jira.mynet.com/rest/api/2/issue/591838",

        "key": "TEST-8564",

        "fields": {

            "summary": "delete  tables 31-03-2020 ",

            "customfield_10006": 2.0,

            "created": "2020-02-27T10:29:12.000+0100",

            "description": "A LOT OF TEXT",

            "assignee": null,

            "labels": [

                "DATA",

                "Refined"

            ],

            "status": {

                "self": "https://jira.mynet.com/rest/api/2/status/10000",

                "description": "",

                "iconUrl": "https://jira.mynet.com/",

                "name": "To Do",

                "id": "10000",

                "statusCategory": {

                    "self": "https://jira.mynet.com/rest/api/2/statuscategory/2",

                    "id": 2,

                    "key": "new",

                    "colorName": "blue-gray",

                    "name": "To Do"

                }

            }

        }

    }

 ]

}

在此示例中,['fields']['assignee'] 中的元素为 NULL,有时如下所示

我正在尝试检查受让人是否为空,如果是这样,请打印空值

SMILET
浏览 114回答 1
1回答

浮云间

尝试if&nbsp;'fields'&nbsp;in&nbsp;each&nbsp;and&nbsp;'assignee'&nbsp;in&nbsp;each['fields']:请注意,您需要密钥的名称,而不是用方括号括起来。也许更好:for&nbsp;each&nbsp;in&nbsp;data['issues']:&nbsp;&nbsp;&nbsp;&nbsp;print(each.get('fields',&nbsp;{}).get('assignee',&nbsp;{}).get('name',&nbsp;'null'))如果您不能保证“问题”存在于以下两个方面:datafor&nbsp;each&nbsp;in&nbsp;data.get('issues',&nbsp;[]): &nbsp;&nbsp;&nbsp;&nbsp;<as&nbsp;before>data.get('issues', [])如果不存在,则返回空列表。data['issuess']
随时随地看视频慕课网APP

相关分类

Python
我要回答