Python,比较JSON迭代与多个键和值

2024-09-18 14:42:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我在比较JSON中的2次迭代以匹配asset Ids和打印所有匹配资产的值时遇到了困难,因为有多个键和多个值让我很困惑,无法有效地做到这一点。在

有什么方法可以比较:

assetId = key

以及

asset = value["asset"]["id"]

从不同的迭代中,如果所有资产中有任何匹配,则打印所有匹配资产编号的(item,listingId,subTotal)?在

不是完整的JSON,而是我需要处理的部分:

{ 
  "success": true,
  "more": false,
  "listinginfo": {
    "719811220882156383": {
      "listingid": "719811220882156383",
      "converted_price": 657,
      "converted_fee": 97,
      "asset": {
        "currency": 0,
        "appid": 730,
        "contextid": "2",
        "id": "4495490578",
        "amount": "1",
      }
    },
    "719811220882158733": {
      "listingid": "719811220882158733",
      "converted_price": 49,
      "converted_fee": 6,
      "asset": {
        "currency": 0,
        "appid": 730,
        "contextid": "2",
        "id": "2778717097",
        "amount": "1",
      }
    },
  "purchaseinfo": [    
  ],
  "assets": {
    "730": {
      "2": {
        "4495490578": {
          "currency": 0,
          "appid": 730,
          "contextid": "2",
          "id": "4495490578",
          "classid": "1389958217",
          "instanceid": "188530139",
          "amount": "1",
          "status": 2,
          "original_amount": "1",
          "background_color": "",
          "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5lpKKqPrxN7LEmyVQ7MEpiLuSrYmnjQO3-UdsZGHyd4_Bd1RvNQ7T_FDrw-_ng5Pu75iY1zI97bhLsvQz",
          "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5lpKKqPrxN7LEm1Rd6dd2j6eQ9N2t2wK3-ENsZ23wcIKRdQE2NwyD_FK_kLq9gJDu7p_KyyRr7nNw-z-DyIFJbNUz",
          "name": "AK-47 | Redline",
          "name_color": "D2D2D2",
          "type": "Classified Rifle",
          "market_name": "AK-47 | Redline (Field-Tested)",
          "market_hash_name": "AK-47 | Redline (Field-Tested)",
          "owner": 0
        },
        "2778717097": {
          "currency": 0,
          "appid": 730,
          "contextid": "2",
          "id": "2778717097",
          "classid": "720294710",
          "instanceid": "188530139",
          "amount": "1",
          "status": 2,
          "original_amount": "1",
          "background_color": "",
          "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgposr-kLAtl7PLZTjlH7du6kb-FlvD1DLPUl31IppF3jLvC8d2i2lKx-BVpN2_3IdPDcwJqMF7Z_VS7yOy-0JbuucvKyiF9-n51aSXb4FU",
          "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgposr-kLAtl7PLZTjlH7du6kb-FlvD1DLPUl31I18lwmO7Eu9jzjga3r0JrYT-mJ9CQJgA6ZA2G-1XqkOjmgpO5tZ-YnHIxv3Yh5izD30vgRbaAlsY",
          "name": "Desert Eagle | Naga",
          "name_color": "D2D2D2",
          "type": "Restricted Pistol",
          "market_name": "Desert Eagle | Naga (Battle-Scarred)",
          "market_hash_name": "Desert Eagle | Naga (Battle-Scarred)",
          "owner": 0
        },
        "4487026121": {
          "currency": 0,
          "appid": 730,
          "contextid": "2",
          "id": "4487026121",
          "classid": "1428441554",
          "instanceid": "0",
          "amount": "1",
          "status": 2,
          "original_amount": "1",
          "background_color": "",
          "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FFMu1aPMI24auITjxteJwPXxY72AkGgIvZAniLjHpon2jlbl-kpvNjz3JJjVLFG9rl1YLQ",
          "tradable": 0,
          "name": "Operation Breakout Weapon Case",
          "name_color": "D2D2D2",
          "type": "Base Grade Container",
          "market_name": "Operation Breakout Weapon Case",
          "market_hash_name": "Operation Breakout Weapon Case",
          "owner": 0
        }
      }
    }
  }
 }
}

到目前为止,我能够进行的分析:

^{pr2}$

不幸的是,这种方法不会比较两个完整列表以匹配资产ID,只有当最后一个资产ID与两个迭代中的第一个资产匹配时。在


Tags: nameidurl资产assetamountmarketappid
1条回答
网友
1楼 · 发布于 2024-09-18 14:42:48

错误地使用for循环。由于两个循环处于同一级别,assetIdimage和{}的值将是上次循环成功运行时分配的值。在

第二个for循环也出现了类似的问题。在

最后,if条件将只比较每个列表中最后一个元素的值。在

解决问题的方法是使用in运算符在正确的字典中查找给定的ID

InfoJson = json.loads(response)
assetInfoJson = InfoJson["assets"]["730"]["2"]
listingInfoJson = InfoJson["listinginfo"]

for key, value in listingInfoJson.iteritems():
    listingId = key
    try:
        subTotal = value["converted_price"]
        feeAmount = value["converted_fee"]
        asset = value["asset"]["id"]
    except KeyError:
      continue

    if asset in assetInfoJson:
        # Get asset details if it's present in the assetInfoJson dict
        assetInfo = assetInfoJson[asset]
        try:
            image = assetInfo["icon_url"]
            item = assetInfo["market_hash_name"]
        except KeyError:
          continue

        print ("Found Matching asset :",assetId)
        print ("Found Matching name : %s, listingid : %s, subtotal : %s" % (item, listingId, subTotal))

相关问题 更多 >