我在比较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与两个迭代中的第一个资产匹配时。在
错误地使用for循环。由于两个循环处于同一级别,}的值将是上次循环成功运行时分配的值。在
assetId
、image
和{第二个for循环也出现了类似的问题。在
最后,if条件将只比较每个列表中最后一个元素的值。在
解决问题的方法是使用
in
运算符在正确的字典中查找给定的ID相关问题 更多 >
编程相关推荐