简体   繁体   English

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

[英]Problem returning data with json.loads, I can't solve it

I'm trying to return a specific value from my json with json.loads and I'm not getting it.我正在尝试使用 json.loads 从我的 json 返回一个特定值,但我没有得到它。 I want it to return the value of 'linkBoleto':我希望它返回“linkBoleto”的值:

This is the structure of my json file这是我的 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"
               }
            }
         }
      ]
   }
}

I tried this, but I couldn't...我试过这个,但我不能...

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

I can return data from the api normally but when I try to apply json.loads to limit the return I can't...我可以正常从 api 返回数据,但是当我尝试应用 json.loads 来限制返回时,我不能...

You can't access "linkBoleto" directly, you need to follow the structure of JSON:不能直接访问"linkBoleto" ,需要遵循JSON的结构:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM