dsegw3424 2015-02-05 21:56
浏览 47

解码JSON - 注意:未定义的偏移量:0

I'm trying to get the value of bitcoin received and the time it was received in the most recent transaction for some bitcoin address using the blockchain.info API. The API displays transaction data in JSON format when requesting a URL like: https://blockchain.info/address/btc_address?format=json.

The response (up until the first transaction) looks like:

{
    "hash160": "-",
    "address": "-",
    "n_tx": 11,
    "total_received": 1,
    "total_sent": 1,
    "final_balance": 1,
    "txs": [
        {
            "ver": 1,
            "inputs": [
                {
                    "sequence": 1,
                    "prev_out": {
                        "spent": true,
                        "tx_index": 1,
                        "type": 0,
                        "addr": "-",
                        "value": 1,
                        "n": 1,
                        "script": "-"
                    },
                    "script": "-"
                }
            ],
            "block_height": 1,
            "relayed_by": "-",
            "out": [
                {
                    "spent": false,
                    "tx_index": 1,
                    "type": 0,
                    "addr": "-",
                    "value": 1337,
                    "n": 0,
                    "script": "-"
                },
                {
                    "spent": true,
                    "tx_index": 1,
                    "type": 0,
                    "addr": "-",
                    "value": 1,
                    "n": 1,
                    "script": "-"
                }
            ],
            "lock_time": 0,
            "result": 0,
            "size": 1,
            "time": 123456,
            "tx_index": 1,
            "vin_sz": 1,
            "hash": "-",
            "vout_sz": 2
        },
    ... more txs ...
    ]
}

I filtered the data because it's not relevant. The only number I want to retrieve is the value 1337 shown in the first out of the first txs and also the time 123456 of the first txs.

So my PHP looks like this:

$result = json_decode(file_get_contents("https://blockchain.info/address/$bitcoinAddress?format=json"));
$lastPayment = $result->txs[0]->out[0]->value;
$lastPaymentTime = $result->txs[0]->time;

This code works fine but I am getting the notice of Undefined offset: 0. I don't want to hide PHP notices but why am I even getting it? Can I not reference the JSON array with indexes? Why not and how else do I get the first element in the JSON array? I don't really want to use a loop...

Thanks!

Edit: My editing of the JSON above might cause some confusion... View the full JSON for a BTC address at https://blockchain.info/address/1NTZMU7yKshvWiZYuMquJUHuzusFLZ8SMZ?format=json

Edit 2: Fixed the problem... One of the BTC addresses I was using caused the notices because it hasn't received any bitcoins and so the txs array was empty...

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡等级和折扣规则
    • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分