Python,比较 JSON 次迭代与多个键和值
Python, comparing JSON iterations with multiple keys and values
我无法比较来自 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
}
}
}
}
}
}
到目前为止我能做的解析:
InfoJson = json.loads(response)
assetInfoJson = InfoJson["assets"]["730"]["2"]
for key, value in assetInfoJson.iteritems():
assetId = key
try:
image = value["icon_url"]
item = value["market_hash_name"]
except KeyError:
continue
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 assetId == asset:
print ("Found Matching asset :",assetId)
print ("Found Matching name : %s, listingid : %s, subtotal : %s" % (item, listingId, subTotal))
不幸的是,这种方式不会比较两个完整列表以匹配资产 ID,仅当最后一个资产 ID 与两次迭代中的第一个资产匹配时才比较。
您错误地使用了 for 循环。由于两个循环处于同一级别,因此 assetId
、image
和 item
的值将是上次循环 运行 成功时分配的值。
第二个 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))
我无法比较来自 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
}
}
}
}
}
}
到目前为止我能做的解析:
InfoJson = json.loads(response)
assetInfoJson = InfoJson["assets"]["730"]["2"]
for key, value in assetInfoJson.iteritems():
assetId = key
try:
image = value["icon_url"]
item = value["market_hash_name"]
except KeyError:
continue
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 assetId == asset:
print ("Found Matching asset :",assetId)
print ("Found Matching name : %s, listingid : %s, subtotal : %s" % (item, listingId, subTotal))
不幸的是,这种方式不会比较两个完整列表以匹配资产 ID,仅当最后一个资产 ID 与两次迭代中的第一个资产匹配时才比较。
您错误地使用了 for 循环。由于两个循环处于同一级别,因此 assetId
、image
和 item
的值将是上次循环 运行 成功时分配的值。
第二个 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))