Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - How to get elements in array flask

I'm doing a flask application. I need to get the elements of the session that are inside an array in mongodb. I tried to do everything, but I was unsuccessful.

I need to get the 'password' inside 'contatos'. To do a validation within my application. I know that if you do the following code: print(session['company'].Get("contatos")), it shows all the content of the object, but I can't access the elements inside that object.

    {
    "nome": "Empresa Teste",
    "cnpj": "00.000.000/0001-00",
    "endereco": {
        "endereco": "Rua, 1",
        "cidade": "Cidade Teste",
        "estado": "Estado Teste",
        "uf": "AA"
    },
    "idMatriz": 1,
    "dtCadastro": "01/01/2020",
    "plano": {
        "id": 1,
        "nome": "Plano de Teste",
        "validade": "01/01/2020"
    },
    "contatos": [{
        "nome": "Fabio",
        "email": "Teste",
        "password": "pass",
        "telefone": "Teste"
    }],
    "contasCadastradas ": [{
        "concessionaria": {
            "id": 1,
            "nome": "EnelSP"
        },
        "identificador": "Numero da Instalacao",
        "cronogramaBusca": {
            "inicio": 1,
            "vencimento": 1,
            "fim": 1
        },
        "metodosCaptura": [{
            "tipoCaptura": "site",
            "ordem": 1,
            "usuario": "user",
            "senha": "pwd",
            "destino": "endereco do site"
        }, {
            "tipoCaptura": "email",
            "ordem": 2,
            "usuario": "user",
            "senha": "pwd",
            "destino": ""
        }]
    }, {
        "concessionaria": {
            "id": 2,
            "nome": "TIM"
        },
        "identificador": "Numero da Instalacao",
        "cronogramaBusca": {
            "inicio": 1,
            "vencimento": 1,
            "fim": 1
        },
        "metodosCaptura   ": [{
            "tipoCaptura": "site",
            "ordem": 1,
            "usuario": "user",
            "senha": "pwd",
            "destino": "endereco do site"
        }, {
            "tipoCaptura": "email",
            "ordem": 2,
            "usuario": "user",
            "senha": "pwd",
            "destino": ""
        }]
    }]
}

can you help me?

question from:https://stackoverflow.com/questions/66050526/how-to-get-elements-in-array-flask

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

1 Reply

0 votes
by (71.8m points)

So if I've read correctly, print(session['company'].Get("contatos")) gives you the output which you have included in the question.

So if you assign that to a variable called data:

data = session['company'].Get("contatos")

You could then access:

>>> data['contatos']
[{'nome': 'Fabio', 'email': 'Teste', 'telefone': 'Teste'}]

That's a list containting a single dictionary, so you could access the first (and only) dictionary in that list:

>>> data['contatos'][0]
{'nome': 'Fabio', 'email': 'Teste', 'telefone': 'Teste'}

And to get an individual field:

>>> data['contatos'][0]['nome']
'Fabio'

I can't see the password field you mention.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...