Python-多个系列JSON数据


   [
   {
    "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 gettern')
local = data[input]
print local;

追溯是:

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

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

最新更新