首页 > 解决方案 > 如何在python中对具有特定键值的复杂json进行排序

问题描述

我正在尝试按“算法”对这些 JSON 数据进行排序。我怎样才能做到这一点?假设我想先按算法值 x11 排序。因此必须首先显示所有具有此算法的对象。

我知道简单的排序,但我不知道从哪里开始。所以我还没有尝试任何东西。请让我知道这是否容易做到?

这是我要排序的 JSON 数据。

{
    "data": {
        "aby": {
            "info": {
                "algo": "scrypt",
                "bestexchange": 4,
                "bestPool": 11,
                "blocks": 1036871,
                "blocktime": 0,
                "coin": "aby",
                "coinsPerDay": "0.66582227581137665151",
                "diff": 5409.10761,
                "diffAlgo": "0.0058443642207875908",
                "hashAlgo": "0",
                "nethash": 113882.060223,
                "pos": "?",
                "priceBTC": "0.00000098",
                "priceUSD": "0.00877766399999999926",
                "reward": 200.037,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "diff",
                "usdPerDay": "0.0058443642207875908",
                "value": "0.0058443642207875908",
                "workers": 311
            },
            "order": 129
        },
        "adz": {
            "info": {
                "algo": "x11",
                "bestexchange": "None",
                "bestPool": 10,
                "blocks": 422399,
                "blocktime": 0,
                "coin": "adz",
                "coinsPerDay": "0.22962182821550283784",
                "diff": 70085.3733,
                "diffAlgo": "0.00450653503837463755",
                "hashAlgo": "0",
                "nethash": 509330.602794,
                "pos": "?",
                "priceBTC": "0.0000022",
                "priceUSD": "0.01962590000000000168",
                "reward": 40,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "diff",
                "usdPerDay": "0.00450653503837463755",
                "value": "0.00450653503837463755",
                "workers": 270
            },
            "order": 138
        },
        "agn": {
            "info": {
                "algo": "neoscrypt",
                "bestexchange": 1,
                "bestPool": 10,
                "blocks": 58768,
                "blocktime": 0,
                "coin": "agn",
                "coinsPerDay": "61.14166600687313746221",
                "diff": 3.75075034,
                "diffAlgo": "1.58813765486204783173",
                "hashAlgo": "0",
                "nethash": 67.403138,
                "pos": "?",
                "priceBTC": "0.0000029",
                "priceUSD": "0.02597471999999999975",
                "reward": 6,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "diff",
                "usdPerDay": "1.58813765486204783173",
                "value": "1.58813765486204783173",
                "workers": 467
            },
            "order": 53
        },
        "agni": {
            "info": {
                "algo": "nist5",
                "bestexchange": 5,
                "bestPool": 10,
                "blocks": 28349,
                "blocktime": 0,
                "coin": "agni",
                "coinsPerDay": "1.04090749113355163225",
                "diff": 1720,
                "diffAlgo": "0.09341846617018165211",
                "hashAlgo": "0",
                "nethash": 57755.561016,
                "pos": "?",
                "priceBTC": "0.00001002",
                "priceUSD": "0.08974713600000000513",
                "reward": 1,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "diff",
                "usdPerDay": "0.09341846617018165211",
                "value": "0.09341846617018165211",
                "workers": 1
            },
            "order": 92
        },
        "akn": {
            "info": {
                "algo": "neoscrypt",
                "bestexchange": 1,
                "bestPool": 3,
                "blocks": 38207,
                "blocktime": 0,
                "coin": "akn",
                "diff": 1,
                "diffAlgo": "0",
                "hashAlgo": "0",
                "nethash": 39.500835,
                "pos": "?",
                "priceBTC": "0.000005001",
                "priceUSD": "0.04479295679999999863",
                "reward": 1.4,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "not enough data",
                "value": "0",
                "workers": 6
            },
            "order": 68
        },
        "alps": {
            "info": {
                "algo": "lyra2z",
                "bestexchange": 1,
                "bestPool": 10,
                "blocks": 33645,
                "blocktime": 0,
                "coin": "alps",
                "coinsPerDay": "120.88746224013561914035",
                "diff": 29.5071345,
                "diffAlgo": "1.32313861223036965953",
                "hashAlgo": "0",
                "nethash": 952.87351636221,
                "pos": "?",
                "priceBTC": "0.000001222",
                "priceUSD": "0.01094520959999999848",
                "reward": 65,
                "timestamp": "2018-05-10 20:56:25.949806",
                "type": "diff",
                "usdPerDay": "1.32313861223036965953",
                "value": "1.32313861223036965953",
                "workers": 46
            },
            "order": 64
        }
    },
    "message": "",
    "status": "ok",
    "timestamp": "Thu, 10 May 2018 20:57:06 GMT"
}

标签: pythonjsonsorting

解决方案


如果在这个项目中可以的话,我建议重新排列你的json数据如下(将更容易索引):

 {
    "data": [{
        "NEWKEY": "aby",
        "info": {
            "algo": "scrypt",
            "bestexchange": 4,
            "bestPool": 11,
            "blocks": 1036871,
            "blocktime": 0,
            "coin": "aby",
            "coinsPerDay": "0.66582227581137665151",
            "diff": 5409.10761,
            "diffAlgo": "0.0058443642207875908",
            "hashAlgo": "0",
            "nethash": 113882.060223,
            "pos": "?",
            "priceBTC": "0.00000098",
            "priceUSD": "0.00877766399999999926",
            "reward": 200.037,
            "timestamp": "2018-05-10 20:56:25.949806",
            "type": "diff",
            "usdPerDay": "0.0058443642207875908",
            "value": "0.0058443642207875908",
            "workers": 311
        },
        "order": 129
    }

更多数据在这里

 ]}

然后:

如何按键对字典进行排序?


推荐阅读