Json解码不工作//不打印

时间:2018-01-04 00:07:00

标签: javascript php json decode

我想要json解码并打印行确认。

我的代码:

  $urls = file_get_contents("https://chain.so/api/v2/address/LTC/3NwuCjaN3cnvq8F1jhD13ikBoVMMYPmh3h");
  $array = json_decode($urls,TRUE);
  // print_r($array,TRUE);
  $confirmation = $array[0]['confirmations'];

问题:为什么我的代码不起作用?

1 个答案:

答案 0 :(得分:1)

这是您从网址获取的json:

{ 
"status" : "success",
"data" : {
"network" : "LTC",
"address" : "3NwuCjaN3cnvq8F1jhD13ikBoVMMYPmh3h",
"balance" : "0.05581900",
"received_value" : "0.05581900",
"pending_value" : "0.00000000",
"total_txs" : 1,
"txs" : [
  {
    "txid" : "94883d8d67f10dcf34b20bf2af5867a9f9dc195f2b6c8d789315fd8b764274a6",
    "block_no" : 1344095,
    "confirmations" : 76,
    "time" : 1515011556,
    "incoming" : {
      "output_no" : 0,
      "value" : "0.05581900",
      "spent" : null,
      "inputs" : [
        {
          "input_no" : 0,
          "address" : "3EguWJJBMpKKyWFq1qgU4SL82uHvdb1HPq",
          "received_from" : {
            "txid" : "08643a119af3312921dbab5eeea8d526f4c6d29dd95cd7a45fb51ea838b87e53",
            "output_no" : 1
          }
        }
      ],
      "req_sigs" : 1,
      "script_asm" : "OP_HASH160 e92b4546a40a63ebdd262596192269c1798d70dc OP_EQUAL",
      "script_hex" : "a914e92b4546a40a63ebdd262596192269c1798d70dc87"
    }
  }
]
},
"code" : 200,
"message" : ""
 }

正如您所看到的那样,您可以看到结构和解码到关联数组,您需要将代码更改为以下内容,以便访问confirmations属性:

$confirmation = $array['data']['txs'][0]['confirmations'];