使用json.loads返回数据时出现问题,无法解决

gopyfrb3  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(107)

我尝试用json.loads从json返回一个特定的值,但是没有得到。我想让它返回'linkBoleto'的值:
这是json文件的结构

{  
   "retorno":{  
      "contasreceber":[  
         {  
            "contareceber":{  
               "id":"13123121",
               "situacao": "aberto", 
               "dataEmissao":"2017-03-09",
               "vencimentoOriginal":"2017-03-09",
               "vencimento":"2017-03-09",
               "competencia":"2017-03-01",
               "nroDocumento":"0123456",
               "valor":15.00,
               "saldo":0.00,
               "historico":"Referente ao pedido de venda nº 423",
               "categoria":"Vendas",
               "idFormaPagamento": "123",
               "portador":"CEF",
               "nroNoBanco":"00100358-56",
               "vendedor":"Vendedor exemplo",
               "pagamento": {
                    "totalPago": 15.00,
                    "totalJuro": 0,
                    "totalDesconto": 0,
                    "totalAcrescimo": 0,
                    "totalTarifa": 0,
                    "data": "2017-03-09"
               },
               "ocorrencia":"Unica",
               "linkBoleto":"https://www.bling.com.br/doc.view.php?132",
               "cliente":{  
                  "nome":"Organisys Software",
                  "cpf_cnpj":"00.000.000/0000-1",
                  "tipoPessoa":"J",
                  "ie_rg":"0000000000",
                  "endereco":"Rua Barão do Rio Branco",
                  "numero":"000",
                  "complemento":"Sala 000",
                  "cidade":"Bento Gonçalves",
                  "bairro":"Centro",
                  "cep":"00.000-000",
                  "uf":"RS",
                  "email":"teste@organisys.com.br"
               }
            }
         }
      ]
   }
}

我试过了,但我不能...

import requests
import json

cnpj_consulta = '00.000.000/0000-1'
api_key = 'my-api-key'
url = 'https://bling.com.br/Api/v2/contasreceber/json/'
f_key ='&apikey='
f_cnpj = '&filters=cnpj'
op = '['
cl = ']'

r = requests.get(f'{url}{f_key}{api_key}{f_cnpj}{op}{cnpj_consulta}{cl}')

y = json.loads (r.content)

print (y ['linkBoleto'])

我可以正常地从API返回数据,但是当我尝试应用json.loads来限制返回时,我不能...

nr7wwzry

nr7wwzry1#

您不能直接访问"linkBoleto",需要遵循JSON的结构:

print(y["retorno"]["contasreceber"][0]["contareceber"]["linkBoleto"])

相关问题