在项目和显示值中搜索JSON

时间:2015-04-17 17:48:08

标签: json python-2.7 search

我有以下JSON数据,我要做的是搜索[lastStarport][Commodities]键并根据商品名称返回其中一个值。

即。我想搜索项目"服装"并返回" cost_mean"价值,我已经看了一些其他搜索JSON的例子,但似乎无法根据我的目的调整它们。

像往常一样,任何帮助都会受到赞赏。

{
"commander": {
    "id": 1111111,
    "name": "xxxxxxxx",
    "credits": 12345678,
    "debt": 0,
    "currentShipId": 2,
    "alive": true,
    "docked": false,
    "rank": {
        "combat": 3,
        "trade": 4,
        "explore": 5,
        "crime": 0,
        "service": 0,
        "empire": 0,
        "federation": 0
    }
},
"lastSystem": {
    "id": "12342356464335",
    "name": "xxxx xxx xxxx"
},
"lastStarport": {
    "id": "234234234234",
    "name": "xxxx xxxxx",
    "faction": "xxxxxxx",
    "commodities": [
        {
            "id": "128049204",
            "name": "Explosives",
            "cost_min": 300,
            "cost_max": 456,
            "cost_mean": "378.00",
            "homebuy": "60",
            "homesell": "56",
            "consumebuy": "4",
            "baseCreationQty": 180.32,
            "baseConsumptionQty": 0,
            "capacity": 49669,
            "buyPrice": 222,
            "sellPrice": 207,
            "meanPrice": 378,
            "demandBracket": 0,
            "stockBracket": 2,
            "creationQty": 49669,
            "consumptionQty": 0,
            "targetStock": 49669,
            "stock": 27813,
            "demand": 0,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Chemicals",
            "volumescale": "1.1400"
        },
        {
            "id": "128049202",
            "name": "Hydrogen Fuel",
            "cost_min": 125,
            "cost_max": 168,
            "cost_mean": "147.00",
            "homebuy": "74",
            "homesell": "71",
            "consumebuy": "3",
            "baseCreationQty": 200,
            "baseConsumptionQty": 200,
            "capacity": 137743,
            "buyPrice": 111,
            "sellPrice": 106,
            "meanPrice": 147,
            "demandBracket": 0,
            "stockBracket": 2,
            "creationQty": 110179,
            "consumptionQty": 27564,
            "targetStock": 117069,
            "stock": 68589,
            "demand": 0,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Chemicals",
            "volumescale": "1.3000"
        },
        {
            "id": "128049203",
            "name": "Mineral Oil",
            "cost_min": 192,
            "cost_max": 325,
            "cost_mean": "259.00",
            "homebuy": "47",
            "homesell": "42",
            "consumebuy": "5",
            "baseCreationQty": 0,
            "baseConsumptionQty": 1585.15,
            "capacity": 873251,
            "buyPrice": 0,
            "sellPrice": 193,
            "meanPrice": 259,
            "demandBracket": 1,
            "stockBracket": 0,
            "creationQty": 0,
            "consumptionQty": 873251,
            "targetStock": 218312,
            "stock": 0,
            "demand": 163734.75,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Chemicals",
            "volumescale": "1.0300"
        },
        {
            "id": "128049241",
            "name": "Clothing",
            "cost_min": 315,
            "cost_max": 474,
            "cost_mean": "395.00",
            "homebuy": "61",
            "homesell": "57",
            "consumebuy": "4",
            "baseCreationQty": 17.85,
            "baseConsumptionQty": 171.5,
            "capacity": 26095,
            "buyPrice": 0,
            "sellPrice": 454,
            "meanPrice": 395,
            "demandBracket": 2,
            "stockBracket": 0,
            "creationQty": 2459,
            "consumptionQty": 23636,
            "targetStock": 8368,
            "stock": 0,
            "demand": 17727,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Consumer Items",
            "volumescale": "1.1500"
        },
        {
            "id": "128049240",
            "name": "Consumer Technology",
            "cost_min": 6561,
            "cost_max": 7500,
            "cost_mean": "7031.00",
            "homebuy": "93",
            "homesell": "92",
            "consumebuy": "1",
            "baseCreationQty": 0,
            "baseConsumptionQty": 27,
            "capacity": 6512,
            "buyPrice": 0,
            "sellPrice": 7503,
            "meanPrice": 7031,
            "demandBracket": 3,
            "stockBracket": 0,
            "creationQty": 0,
            "consumptionQty": 6512,
            "targetStock": 1627,
            "stock": 0,
            "demand": 4885,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Consumer Items",
            "volumescale": "1.1000"
        },
        {
            "id": "128049238",
            "name": "Domestic Appliances",
            "cost_min": 527,
            "cost_max": 734,
            "cost_mean": "631.00",
            "homebuy": "70",
            "homesell": "67",
            "consumebuy": "3",
            "baseCreationQty": 10.71,
            "baseConsumptionQty": 102.41,
            "capacity": 9709,
            "buyPrice": 0,
            "sellPrice": 692,
            "meanPrice": 631,
            "demandBracket": 2,
            "stockBracket": 0,
            "creationQty": 1476,
            "consumptionQty": 8233,
            "targetStock": 3534,
            "stock": 0,
            "demand": 6175,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Consumer Items",
            "volumescale": "1.2500"
        },
        {
            "id": "128049177",
            "name": "Algae",
            "cost_min": 135,
            "cost_max": 265,
            "cost_mean": "200.00",
            "homebuy": "27",
            "homesell": "20",
            "consumebuy": "7",
            "baseCreationQty": 0,
            "baseConsumptionQty": 642.6,
            "capacity": 81180,
            "buyPrice": 0,
            "sellPrice": 136,
            "meanPrice": 200,
            "demandBracket": 1,
            "stockBracket": 0,
            "creationQty": 0,
            "consumptionQty": 81180,
            "targetStock": 20295,
            "stock": 0,
            "demand": 15221.25,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Foods",
            "volumescale": "1.0000"
        },
        {
            "id": "128049182",
            "name": "Animal Meat",
            "cost_min": 1286,
            "cost_max": 1633,
            "cost_mean": "1460.00",
            "homebuy": "81",
            "homesell": "79",
            "consumebuy": "2",
            "baseCreationQty": 0,
            "baseConsumptionQty": 97,
            "capacity": 12032,
            "buyPrice": 0,
            "sellPrice": 1287,
            "meanPrice": 1460,
            "demandBracket": 1,
            "stockBracket": 0,
            "creationQty": 0,
            "consumptionQty": 12032,
            "targetStock": 3008,
            "stock": 0,
            "demand": 2256,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Foods",
            "volumescale": "1.4000"
        },
        {
            "id": "128049189",
            "name": "Coffee",
            "cost_min": 1286,
            "cost_max": 1633,
            "cost_mean": "1460.00",
            "homebuy": "81",
            "homesell": "79",
            "consumebuy": "2",
            "baseCreationQty": 0,
            "baseConsumptionQty": 97,
            "capacity": 3900,
            "buyPrice": 0,
            "sellPrice": 1287,
            "meanPrice": 1460,
            "demandBracket": 1,
            "stockBracket": 0,
            "creationQty": 0,
            "consumptionQty": 3900,
            "targetStock": 974,
            "stock": 0,
            "demand": 731.5,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Foods",
            "volumescale": "1.4000"
        },
        {
            "id": "128049183",
            "name": "Fish",
            "cost_min": 403,
            "cost_max": 583,
            "cost_mean": "493.00",
            "homebuy": "66",
            "homesell": "63",
            "consumebuy": "3",
            "baseCreationQty": 0,
            "baseConsumptionQty": 271,
            "capacity": 35482,
            "buyPrice": 0,
            "sellPrice": 404,
            "meanPrice": 493,
            "demandBracket": 1,
            "stockBracket": 0,
            "creationQty": 0,
            "consumptionQty": 35482,
            "targetStock": 8870,
            "stock": 0,
            "demand": 6653,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Foods",
            "volumescale": "1.2000"
        },
        {
            "id": "128049184",
            "name": "Food Cartridges",
            "cost_min": 141,
            "cost_max": 270,
            "cost_mean": "206.00",
            "homebuy": "31",
            "homesell": "24",
            "consumebuy": "7",
            "baseCreationQty": 230.52,
            "baseConsumptionQty": 222.5,
            "capacity": 33794,
            "buyPrice": 44,
            "sellPrice": 34,
            "meanPrice": 206,
            "demandBracket": 0,
            "stockBracket": 3,
            "creationQty": 31749,
            "consumptionQty": 2045,
            "targetStock": 32259,
            "stock": 30540.025009,
            "demand": 0,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Foods",
            "volumescale": "1.0000"
        },
        {
            "id": "128049178",
            "name": "Fruit And Vegetables",
            "cost_min": 315,
            "cost_max": 474,
            "cost_mean": "395.00",
            "homebuy": "61",
            "homesell": "57",
            "consumebuy": "4",
            "baseCreationQty": 0,
            "baseConsumptionQty": 350,
            "capacity": 14069,
            "buyPrice": 0,
            "sellPrice": 316,
            "meanPrice": 395,
            "demandBracket": 1,
            "stockBracket": 0,
            "creationQty": 0,
            "consumptionQty": 14069,
            "targetStock": 3516,
            "stock": 0,
            "demand": 2638.25,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Foods",
            "volumescale": "1.1500"
        },
        {
            "id": "128049180",
            "name": "Grain",
            "cost_min": 207,
            "cost_max": 342,
            "cost_mean": "275.00",
            "homebuy": "50",
            "homesell": "45",
            "consumebuy": "5",
            "baseCreationQty": 0,
            "baseConsumptionQty": 584,
            "capacity": 80485,
            "buyPrice": 0,
            "sellPrice": 208,
            "meanPrice": 275,
            "demandBracket": 1,
            "stockBracket": 0,
            "creationQty": 0,
            "consumptionQty": 80485,
            "targetStock": 20120,
            "stock": 0,
            "demand": 15091.25,
            "rare_min_stock": "0",
            "rare_max_stock": "0",
            "market_id": null,
            "parent_id": null,
            "statusFlags": [],
            "categoryname": "Foods",
            "volumescale": "1.0500"
        }
    ]
}
}

1 个答案:

答案 0 :(得分:0)

在查看了我的一些示例之后,我意识到我只是一个关键,需要解析JSON列表的密钥

这实现了我的目标:

data = json.loads(JSON_String)['lastStarport']
com = raw_input('Commodity:')
    comPrice = [item["cost_mean"] for item in data['commodities']
    if item['name'] == com]
print comPrice