-1

I accessed Roblox API to get the list of friends. Everything seems to work fine until I tried printing the username..

Console Error:

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

Code:

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"])

martineau
  • 119,623
  • 25
  • 170
  • 301
Erik
  • 35
  • 8
  • 3
    Does this answer your question? [Why am I seeing "TypeError: string indices must be integers"?](https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers) – AMC Mar 27 '20 at 18:56

1 Answers1

1

You don't need change user at all, since r.json() already returns a dictionary:

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"])
AdamGold
  • 4,941
  • 4
  • 29
  • 47