尝试获取json对象时,字符串索引必须是整数

2024-10-17 08:29:03 发布

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

我访问了Roblox API以获取朋友列表。在我尝试打印用户名之前,一切似乎都很正常

控制台错误:

Please enter Username/UserID of the player: 1517232281
Status: 200
Response:
[{"Id":189675950,"Username":"alona_0627","AvatarUri":"https://tr.rbxcdn.com/01f752ec70c25b9c6144cca6f81b410e/30/30/Avatar/Jpeg","AvatarFinal":true,"IsOnline":true},{"Id":970243365,"Username":"fgarfgasFdffs","AvatarUri":"https://tr.rbxcdn.com/58fc9b6c4f1059f2d3634df36f37f12d/30/30/Avatar/Jpeg","AvatarFinal":true,"IsOnline":false},{"Id":1499755935,"Username":"muhmuhmh","AvatarUri":"https://tr.rbxcdn.com/8ed4d50a0c080c2ddce960d3c65dc506/30/30/Avatar/Jpeg","AvatarFinal":true,"IsOnline":false},{"Id":158543991,"Username":"trikydragon_master2","AvatarUri":"https://tr.rbxcdn.com/565ad229fd89f62ca679d43ada4dd8ba/30/30/Avatar/Jpeg","AvatarFinal":true,"IsOnline":false}]
[{'Id': 189675950, 'Username': 'alona_0627', 'AvatarUri': 'https://tr.rbxcdn.com/01f752ec70c25b9c6144cca6f81b410e/30/30/Avatar/Jpeg', 'AvatarFinal': True, 'IsOnline': True}, {'Id': 970243365, 'Username': 'fgarfgasFdffs', 'AvatarUri': 'https://tr.rbxcdn.com/58fc9b6c4f1059f2d3634df36f37f12d/30/30/Avatar/Jpeg', 'AvatarFinal': True, 'IsOnline': False}, {'Id': 1499755935, 'Username': 'muhmuhmh', 'AvatarUri': 'https://tr.rbxcdn.com/8ed4d50a0c080c2ddce960d3c65dc506/30/30/Avatar/Jpeg', 'AvatarFinal': True, 'IsOnline': False}, {'Id': 158543991, 'Username': 'trikydragon_master2', 'AvatarUri': 'https://tr.rbxcdn.com/565ad229fd89f62ca679d43ada4dd8ba/30/30/Avatar/Jpeg', 'AvatarFinal': True, 'IsOnline': False}]

Traceback (most recent call last):
  File "main.py", line 17, in <module>
    options()
  File "main.py", line 13, in options
    rbxl()
  File "E:\ESpolit\options\roblox.py", line 42, in rbxl
    gatherlist()
  File "E:\ESpolit\options\roblox.py", line 22, in gatherlist
    print(jsonl["Username"][0])
TypeError: string indices must be integers

代码:

def gatherlist():
    clear()
    art("Friend Lister", "true")
    print("\n\n\n")
    userni = input("Please enter Username/UserID of the player: ")
    if strnum(userni) == "id":
        r = requests.get('https://api.roblox.com/users/'+userni+'/friends')
        print("Status: "+str(r.status_code))
        print("Response: "+ r.text)
        rese21 = r.json()
        for user in rese21:
            jsonl = json.dumps(rese21)

            print(rese21, "\n")
            print(jsonl["Username"])


Tags: inhttpscomidtrueusernametrjpeg
1条回答
网友
1楼 · 发布于 2024-10-17 08:29:03

您根本不需要更改user,因为r.json()已经返回了一个字典:

def gatherlist():
    clear()
    art("Friend Lister", "true")
    print("\n\n\n")
    userni = input("Please enter Username/UserID of the player: ")
    if strnum(userni) == "id":
        r = requests.get('https://api.roblox.com/users/'+userni+'/friends')
        print("Status: "+str(r.status_code))
        print("Response: "+ r.text)
        rese21 = r.json()
        for user in rese21:
            print(user, "\n")
            print(user["Username"])

相关问题 更多 >