Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

python - TypeError: string indices must be integers Json.py

I get the error "TypeError: string indices must be integers" in the following code:

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

Body:

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

Full Error:

    if body['category'] == 'Character':
TypeError: string indices must be integers
question from:https://stackoverflow.com/questions/65865234/typeerror-string-indices-must-be-integers-json-py

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Verify your body variable type, the reason why you're getting TypeError: string indices must be integers is because the body is of a string type and only integers indices can be used on a string.

You need to either convert your variable to a dict type where string indices can be used or ensure the variable you received is of a dict type


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...