Python - 多个JSON数据系列

时间:2014-03-07 21:41:28

标签: python json parsing

   [
   {
    "account" : "",
    "address" : "DD741HcHii8vq4fzPH58UDU7virAvtYyJf",
    "category" : "send",
    "amount" : -9.00000000,
    "fee" : -1.00000000,
    "confirmations" : 316,
    "blockhash" : "9546b8d336c74222040b85a0a760c4c3dc4d5b744e0072fc1551f56f20472739",
    "blockindex" : 31,
    "blocktime" : 1394208201,
    "txid" : "45f80847a45eab62189759eb9da30f40052c581ea06ca062ac155bbf563b907d",
    "time" : 1394208153,
    "timereceived" : 1394208153
    },
    {
    "account" : "",
    "address" : "DD741HcHii8vq4fzPH58UDU7virAvtYyJf",
    "category" : "send",
    "amount" : -0.04063000,
    "fee" : -2.00000000,
    "confirmations" : 313,
    "blockhash" : "27cd2b1f380a651f78f83ef4deaaaf6a220028fe09b4219207ad5efecc69a29f",
    "blockindex" : 7,
    "blocktime" : 1394208392,
    "txid" : "af48a278c3559f1c36a2fccda42ba35cc62c2083fa5959a567f6b4d4a4a594a7",
    "time" : 1394208388,
    "timereceived" : 1394208388
    }
    ]

我有一些数据被称为text.json

我怎么能读到这个?

我尝试将其解析为普通的JSON但收到错误:

    "list indices must be integer, not str"

而且,如果我找到一种阅读方式,我可以这样做:

getAmountOfRecords("blockindex")
JSON.parse[blockindex[0]] (returning 31)
JSON.parse[blockindex[1]] (returning 7)

谢谢:)

编辑:

我的代码是:

import os
import json
from pprint import pprint

os.system("dogecoind listtransactons > text.json")
with open('text.json') as f:
        data = json.load(f)
input = raw_input('Enter a getter\n')
local = data[input]
print local;

回溯是:

File "th.py", line 12, in <module>
local = data[input]
TypeError: list indices must be integers, not str

1 个答案:

答案 0 :(得分:2)

问题不在于解析,而在于访问解析后的数据。您的数据是一个dicts列表。因此,您无法直接使用字符串键访问它。 data['blockindex']不存在:仅data[0]['blockindex']data[1]['blockindex'],等等。