如何从JSON文件中提取这些字段

时间:2016-11-06 23:00:59

标签: python json

{
     "hash": "000000000000000004222f6adb061d2757bc956183e04d2683d3ca7ffdb4be33",
     "confirmations": 26,
     "strippedsize": 749127,
     "size": 749127,
     "weight": 2996508,
     "height": 437643,
     "version": 536870912,
     "versionHex": "20000000",
     "merkleroot": "e325487f1a017ad7007f1a23fe65df27a819946e4e5f9e5f574fe05adb063c2a",
     "tx": [
    {
      "txid": "e823cacd74b1f67a6ad0c1c18175aa6abc63abb72ae8c3f80e8e9482549c3ba0",
      "hash": "e823cacd74b1f67a6ad0c1c18175aa6abc63abb72ae8c3f80e8e9482549c3ba0",
      "size": 142,
      "vsize": 142,
      "version": 2,
      "locktime": 0,
      "vin": [
        {
          "coinbase": "038bad062cfabe6d6df4d1e2c7d4c17c6a9dc3c8a93ef06328708843c6f36382819ba994aff619c0b702000000000000007be1fc0185ca8301",
          "sequence": 0
        }
      ],
      "vout": [
        {
          "value": 13.12607463,
          "n": 0,
          "scriptPubKey": {
            "asm": "OP_DUP OP_HASH160 66ffbcd106a4aa918da0dee98b09c5d413c09a3f OP_EQUALVERIFY OP_CHECKSIG",
            "hex": "76a91466ffbcd106a4aa918da0dee98b09c5d413c09a3f88ac",
            "reqSigs": 1,
            "type": "pubkeyhash",
            "addresses": [
              "1APcHXQPWJtA8kCz4f9w7ysLngdXgwhPtX"
            ]
          }
        }
      ]
    }
  ]
}

所以我试图提取字段“txid:”,“value:”和“addresses:”的值,我可以提取字段txid和value,但是我无法提取地址字段,我只是不能到达地址字段。这是我在python中编写的代码

jdata = json.loads(open('rawtrans.json').read())

for x in range(0,2):
for c in jdata['tx']:
    print 'txid: {}'.format(c.get('txid'))

for c in jdata['tx'][x]['vout']:
    print 'value: {}'.format(c.get('value'))

for c in jdata['tx']['vout'][x]['scriptPubKey']:
    print 'addresses: {}'.format(c.get('addresses')) 

0 个答案:

没有答案