如何从python中复杂的Json中提取数据

时间:2018-05-10 18:29:39

标签: python json

我正在尝试从python中的JSON中提取特定数据,但是如果没有指定名称aby,adz,agn就可以这样做,因为数据数据非常大而我只发布了部分数据。

例如,我想得到" nethash"其中"顺序"是160.这里最好的策略是什么?

这是我的JSON:

{
"data": {
    "aby": {
        "info": {
            "algo": "scrypt",
            "bestPool": 11,
            "bestexchange": 4,
            "blocks": 1036520,
            "blocktime": 0,
            "coin": "aby",
            "coinsPerDay": "0.82148358090787920727",
            "diff": 4383.33555,
            "diffAlgo": "0.00753414531332241653",
            "hashAlgo": "0",
            "nethash": 66289.728291,
            "pos": "?",
            "priceBTC": "0.00000098",
            "priceUSD": "0.00917138879999999934",
            "reward": 200,
            "timestamp": "2018-05-10 08:51:02.782957",
            "type": "diff",
            "usdPerDay": "0.00753414531332241653",
            "value": "0.00753414531332241653",
            "workers": 757
        },
        "order": 109
    },
    "adz": {
        "info": {
            "algo": "x11",
            "bestPool": 10,
            "bestexchange": "None",
            "blocks": 422294,
            "blocktime": 0,
            "coin": "adz",
            "coinsPerDay": "0.15507838730965944896",
            "diff": 103774.174,
            "diffAlgo": "0.00310020305638486395",
            "hashAlgo": "0",
            "nethash": 612234.455356,
            "pos": "?",
            "priceBTC": "0.00000214",
            "priceUSD": "0.01999120000000000064",
            "reward": 40,
            "timestamp": "2018-05-10 08:51:02.782957",
            "type": "diff",
            "usdPerDay": "0.00310020305638486395",
            "value": "0.00310020305638486395",
            "workers": 265
        },
        "order": 160
    },
    "agn": {
        "info": {
            "algo": "neoscrypt",
            "bestPool": 10,
            "bestexchange": 5,
            "blocks": 58301,
            "blocktime": 0,
            "coin": "agn",
            "coinsPerDay": "51.22860596982359027152",
            "diff": 4.47654431,
            "diffAlgo": "1.47183776684280331892",
            "hashAlgo": "0",
            "nethash": 86.217988,
            "pos": "?",
            "priceBTC": "0.00000307",
            "priceUSD": "0.02873077919999999716",
            "reward": 6,
            "timestamp": "2018-05-10 08:51:02.782957",
            "type": "diff",
            "usdPerDay": "1.47183776684280331892",
            "value": "1.47183776684280331892",
            "workers": 417
        },
        "order": 61
    }
},
"message": "",
"status": "ok",
"timestamp": "Thu, 10 May 2018 08:51:07 GMT"

}

2 个答案:

答案 0 :(得分:0)

最好的策略是由您定义:

您可以使用 objectpath 等库,它可以让您轻松搜索JSON。 只需导入库并构建对象树,然后键入要搜索的单词。

你也可以在json中建立自己的classfunction进行搜索,有些像:

obj = CustomJsonSearch(..{your data})
query = obj.return_value(search_id=168, retunr_params=['nethash'])

或使用像 postgres 这样的数据库 并提出疑问。

答案 1 :(得分:0)

您可以迭代JSON的键,直到找到您要搜索的内容:

data = json_data['data']
for i in data.keys():
    if data[i]['order'] == 160:
        nethash = data[i]['info']['nethash']

编辑:这是一些用于搜索多个值的可能代码,我测试了它并且它有效,但不确定它对大型数据集的效率如何:

data = json_data['data']
search_list = [160, 61]
nethash_dict = {}
for i in data.keys():
    if data[i]['order'] in search_list:
        nethash_dict[data[i]['order']] = data[i]['info']['nethash']
相关问题