I have a json list like this:
{
"usuarios": [
{
"id": 1,
"nome": "vitor",
"email": "vitor@"
}
]
}
And I try to list this items using Python like this:
#!/usr/bin/python
import requests
import json
import sys
def list_users():
response = json.loads(requests.get("http://127.0.0.1:3000/usuarios/")._content)
for r in response.get("usuarios"):
print r["id"],r["nome"],r['email']
if __name__ == '__main__':
list_users()
And when I run, appear this error:
python rest_cli.py
Traceback (most recent call last):
File "rest_cli.py", line 62, in <module>
listar_usuarios()
File "rest_cli.py", line 10, in listar_usuarios
for r in response.get("usuarios"):
AttributeError: 'list' object has no attribute 'get'
How can I fix it?
responsebefore looping and post that hereresponseis apparently a list, so it has notgetmethod like you try to use inresponse.get("usuarios"). Instead, probablyfor r in response: ....