TypeError:字符串索引必须是整数Json.py

2024-05-14 00:10:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我在以下代码中得到错误“TypeError:字符串索引必须是整数”:

if body['category'] == 'Character':
        return sanic.response.json([], status=200)

正文:

{
        "lockerItem": "sandbox_loadout",
        "category": "Character",
        "itemToSlot": "AthenaCharacter:CID_279_Athena_Commando_M_TacticalSanta",
        "slotIndex": 0,
        "variantUpdates": [
                {
                        "channel": "Parts",
                        "active": "Stage0",
                        "owned": []
                }
        ],
        "optLockerUseCountOverride": -1
}

完全错误:

    if body['category'] == 'Character':
TypeError: string indices must be integers

Tags: 字符串代码jsonreturnifsanicresponsestatus
1条回答
网友
1楼 · 发布于 2024-05-14 00:10:48

验证body变量类型,之所以得到TypeError: string indices must be integers 是因为body是字符串类型,并且只能对字符串使用整数索引

您需要将变量转换为可以使用字符串索引的dict类型,或者确保收到的变量为dict类型

相关问题 更多 >