请教大佬们关于python解析嵌套json的问题
{"code": 200,
"message": "操作成功",
"data": {
"pageNum": 0,
"pageSize": 10,
"totalPage": 1215,
"total": 12148,
"list": [{
"id": 270208,
"gameAccountQufu": "踏北尘|紫禁之巅",
"productSn": "NSH26171344",
"brandId": 6,
"brandName": "逆水寒手游",
"productCategoryId": 75,
"productCategoryName": "逆水寒手游",
"createTime": "2024-08-15T11:56:29.000+00:00",
"publishTime": "2024-08-16T05:19:57.000+00:00",
"pic": "https://images.kkzhw.com/storage/uploads/im/20240815/c8d2e273c6bc2994fd2387723de971c5.png",
"price": 118887.0,
"sale": 0,
"newStatus": 0,
"recommandStatus": 0,
"stock": 9,
"promotionType": 0,
"attrValueList": [{
"id": 17538310,
"productAttributeId": 113,
"value": "踏北尘|紫禁之巅",
"intValue": -1,
"values": [
"踏北尘|紫禁之巅"
],
"type": 1,
"name": "区服",
"ename": "gameAccountQufu",
"selectType": 0,
"inputType": 0,
"filterType": 0,
"searchType": 0,
"relatedStatus": 0,
"handAddStatus": 0,
"searchSort": 0
},
{
"id": 17538311,
"productAttributeId": 374,
"value": "682201",
"intValue": 682201,
"values": [
"682201"
],
"type": 1,
"name": "充值金额",
"ename": null,
"selectType": 0,
"inputType": 0,
"filterType": 0,
"searchType": 0,
"relatedStatus": 0,
"handAddStatus": 0,
"searchSort": 0
},
{
"id": 17538312,
"productAttributeId": 344,
"value": "沧溟荡流[核],碧澜解语[核],寰宇流光[核],白夜拂雪,星剑分野,星落月皓,玄龙耀世[钱]",
"intValue": -1,
"values": [
"沧溟荡流[核]",
"碧澜解语[核]",
"寰宇流光[核]",
"白夜拂雪",
"星剑分野",
"星落月皓",
"玄龙耀世[钱]"
],
"type": 2,
"name": "稀有外观",
"ename": null,
"selectType": 0,
"inputType": 0,
"filterType": 0,
"searchType": 0,
"relatedStatus": 0,
"handAddStatus": 0,
"searchSort": 0
},
{
"id": 17538319,
"productAttributeId": 107,
"value": "否",
"intValue": -1,
"values": [
"否"
],
"type": 1,
"name": "转职CD",
"ename": null,
"selectType": 0,
"inputType": 0,
"filterType": 0,
"searchType": 0,
"relatedStatus": 0,
"handAddStatus": 0,
"searchSort": 0
},
{
"id": 17538320,
"productAttributeId": 108,
"value": "邮箱账号",
"intValue": -1,
"values": [
"邮箱账号"
],
"type": 1,
"name": "账号类型",
"ename": null,
"selectType": 0,
"inputType": 0,
"filterType": 0,
"searchType": 0,
"relatedStatus": 0,
"handAddStatus": 0,
"searchSort": 0
}
],
"sort": 1,
"sortValue1": 140370,
"sortValue2": 656620,
"sortValue3": 299999,
"sortValue4": 0,
"sortValue5": 163,
"sortValue6": 0,
"sortValue7": 0,
"sortValue8": 0,
"sortValue9": 0,
"gameGoodsFangxin": 0,
"gameGoodsBukuan": 0,
"gameGoodsJiangjia": 0,
"gameGoodsYijia": 1,
"gameGoodsYishou": 0,
"gameGoodsYuyue": 0,
"gameGoodsSaletype": 0,
"gameGoodsTop": 0,
"gameSysinfoReadcount": 39,
"gameSysinfoCollectcount": 2,
"description": "玄机 【邮箱账号】账号描述:双顶奢双15w乘风登玉京,额度满没用过,大衣柜时装齐极品灵韵内功毕业首领打造,进度T0一键25w上号即玩,6灵韵,极品内功很多就不一一截了,有想要的细节联系客服截图给你爽快送包赔 充值金额:682201 稀有外观:沧溟荡流 碧澜解语 寰宇流光 白夜拂雪 星剑分野 星落月皓 玄龙耀世 天赏祥瑞:灵猊衔威舆 流光溯 南山雪 极品内功:昆吾断玉 绝电飞光 俯仰太虚 荡剑扬波 日月两仪 不动明王 万化冥合 ",
"name": "NSH26171344区服:踏北尘|紫禁之巅 充值金额:682201 稀有外观:沧溟荡流[核],碧澜解语[核],寰宇流光[核],白夜拂雪,星剑分野,星落月皓,玄龙耀世[钱] 天赏祥瑞:灵猊衔威舆[钱],流光溯[钱],南山雪 极品内功:昆吾断玉[核],绝电飞光[核],俯仰太虚[核],荡剑扬波[核],日月两仪[核],不动明王[核],万化冥合[核] 评分:299999 衣品:656620 性别:男 转性CD:否 转职CD:否【邮箱账号】 职业:玄机 已使用天赏石:163 国色值:140370 天霓染:10 灵韵数量:6",
"subTitle": "NSH26171344区服:踏北尘|紫禁之巅 充值金额:682201 稀有外观:沧溟荡流[核],碧澜解语[核],寰宇流光[核],白夜拂雪,星剑分野,星落月皓,玄龙耀世[钱] 天赏祥瑞:灵猊衔威舆[钱],流光溯[钱],南山雪 极品内功:昆吾断玉[核],绝电飞光[核],俯仰太虚[核],荡剑扬波[核],日月两仪[核],不动明王[核],万化冥合[核] 评分:299999 衣品:656620 性别:男 转性CD:否 转职CD:否【邮箱账号】 职业:玄机 已使用天赏石:163 国色值:140370 天霓染:10 灵韵数量:6",
"keywords": "NSH26171344区服:踏北尘|紫禁之巅 充值金额:682201 稀有外观:沧溟荡流[核],碧澜解语[核],寰宇流光[核],白夜拂雪,星剑分野,星落月皓,玄龙耀世[钱] 天赏祥瑞:灵猊衔威舆[钱],流光溯[钱],南山雪 极品内功:昆吾断玉[核],绝电飞光[核],俯仰太虚[核],荡剑扬波[核],日月两仪[核],不动明王[核],万化冥合[核] 评分:299999 衣品:656620 性别:男 转性CD:否 转职CD:否 账号类型:邮箱账号 职业:玄机 已使用天赏石:163 国色值:140370 天霓染:10 灵韵数量:6 账号专区:极品账号专区 【逆水寒手游】 26171344 NSH26171344 玄机 【邮箱账号】账号描述:双顶奢双15w乘风登玉京,额度满没用过,大衣柜时装齐极品灵韵内功毕业首领打造,进度T0一键25w上号即玩,6灵韵,极品内功很多就不一一截了,有想要的细节联系客服截图给你爽快送包赔 充值金额:682201 稀有外观:沧溟荡流 碧澜解语 寰宇流光 白夜拂雪 星剑分野 星落月皓 玄龙耀世 天赏祥瑞:灵猊衔威舆 流光溯 南山雪 极品内功:昆吾断玉 绝电飞光 俯仰太虚 荡剑扬波 日月两仪 不动明王 万化冥合 ",
"tagsKKList": null
}
{
"id": 270604,
"gameAccountQufu": "踏北尘|瑶光听雪",
"productSn": "NSH11849511",
"brandId": 6,
"brandName": "逆水寒ol",
"productCategoryId": 75,
"productCategoryName": "逆水寒手游",
"createTime": "2024-08-16T01:38:05.000+00:00",
"publishTime": "2024-08-16T05:20:35.000+00:00",
"pic": "https://images.kkzhw.com/storage/uploads/imgPc/20240816/8091f7d77d351aa869a43bc271b2239c.png",
"price": 1000.0,
"sale": 0,
"newStatus": 0,
"recommandStatus": 0,
"stock": 9,
"promotionType": 0,
"attrValueList": [{
"id": 17567027,
"productAttributeId": 108,
"value": "手机账号",
"intValue": -1,
"values": [
"手机账号"
],
"type": 1,
"name": "账号类型",
"ename": null,
"selectType": 0,
"inputType": 0,
"filterType": 0,
"searchType": 0,
"relatedStatus": 0,
"handAddStatus": 0,
"searchSort": 0
},
{
"id": 17567028,
"productAttributeId": 105,
"value": "男",
"intValue": -1,
"values": [
"男"
],
"type": 1,
"name": "性别",
"ename": null,
"selectType": 0,
"inputType": 0,
"filterType": 0,
"searchType": 0,
"relatedStatus": 0,
"handAddStatus": 0,
"searchSort": 0
},
{
"id": 17567029,
"productAttributeId": 114,
"value": "神相",
"intValue": -1,
"values": [
"神相"
],
"type": 1,
"name": "职业",
"ename": null,
"selectType": 0,
"inputType": 0,
"filterType": 0,
"searchType": 0,
"relatedStatus": 0,
"handAddStatus": 0,
"searchSort": 0
},
{
"id": 17567030,
"productAttributeId": 852,
"value": "无",
"intValue": -1,
"values": [
"无"
],
"type": 1,
"name": "换绑CD",
"ename": null,
"selectType": 0,
"inputType": 0,
"filterType": 0,
"searchType": 0,
"relatedStatus": 0,
"handAddStatus": 0,
"searchSort": 0
},
{
"id": 17567031,
"productAttributeId": 107,
"value": "否",
"intValue": -1,
"values": [
"否"
],
"type": 1,
"name": "转职CD",
"ename": null,
"selectType": 0,
"inputType": 0,
"filterType": 0,
"searchType": 0,
"relatedStatus": 0,
"handAddStatus": 0,
"searchSort": 0
},
],
"sort": 1,
"sortValue1": 0,
"sortValue2": 47035,
"sortValue3": 282733,
"sortValue4": 0,
"sortValue5": 3,
"sortValue6": 0,
"sortValue7": 0,
"sortValue8": 0,
"sortValue9": 0,
"gameGoodsFangxin": 0,
"gameGoodsBukuan": 0,
"gameGoodsJiangjia": 0,
"gameGoodsYijia": 0,
"gameGoodsYishou": 0,
"gameGoodsYuyue": 0,
"gameGoodsSaletype": 0,
"gameGoodsTop": 0,
"gameSysinfoReadcount": 1,
"gameSysinfoCollectcount": 0,
"description": "神相 【手机账号】充值金额:2304 天赏发型:喵语 紫宸 武器轻功外观:伞梦 疾鹰掠野 ",
"name": "NSH11849511 【手机账号】 性别:男 职业:神相 没有换绑CD 有转职CD 没有转性CD 已使用天赏石:3 天霓染:0 区服:踏北尘|瑶光听雪",
"subTitle": "NSH11849511 【手机账号】 性别:男 职业:神相 没有换绑CD 有转职CD 没有转性CD 已使用天赏石:3 天霓染:0 区服:踏北尘|瑶光听雪",
"keywords": "NSH11849511账号类型:手机账号 性别:男 职业:神相 换绑CD:无 转职CD:有 转性CD:无 已使用天赏石:3 天霓染:0 区服:踏北尘|瑶光听雪 【逆水寒手游】 11849511 NSH11849511 神相 【手机账号】充值金额:2304 天赏发型:喵语 紫宸 武器轻功外观:伞梦 疾鹰掠野 ",
"tagsKKList": null
}
}
请教下大佬们打个比方这段json,怎么循环去获取每一组数据里对应productAttributeId的值,再根据这个对应的值去获取对应value值,对应的id有则获取值无则跳过,请问要怎么实现呢 import json
with open('data.json','r',encoding='utf8') as fr:
data = json.loads(fr.read())
data_list = data["data"]["list"]
for lst in data_list:
print(f"区服:{lst['gameAccountQufu']}")
attr_value_list = lst["attrValueList"]
for attr_value in attr_value_list:
if "productAttributeId" in attr_value:
print(f'productAttributeId:{attr_value["productAttributeId"]}')
print(f'name:{attr_value["name"]}\tvalue:{attr_value["value"]}')
print("-"*50) 不知道你要的是不是这种效果?{:1_924:}
import json
json_data = "你的json文本数据"
data = json.loads(json_data)
for product in data["data"]["list"]:
# 获取属性列表
attributes = product.get("attrValueList", [])
for attribute in attributes:
product_attribute_id = attribute.get("productAttributeId")
value = attribute.get("value")
# productAttributeId和value
print(f"productAttributeId: {product_attribute_id}, value: {value}")
# 如果存在id字段,则获取其值
if "id" in attribute:
attribute_id = attribute.get("id")
print(f"Attribute ID: {attribute_id}")
YuYu798 发表于 2024-8-16 14:26
不知道你要的是不是这种效果?
import json
差不多,我自己再处理一下加工一下,感谢大佬 WXjzc 发表于 2024-8-16 14:17
import json
with open('data.json','r',encoding='utf8') as fr:
感谢大佬,很清晰 WXjzc 发表于 2024-8-16 14:17
import json
with open('data.json','r',encoding='utf8') as fr:
大佬,再请教下,假如我要把这些数据放到json里面,然后根据对应的productAttributeId去获取对应的值赋值给某个字段,这个应该要怎么写 直接找到对应的对象,然后obj["你要新增或修改的字段"]=对应的值,就可以,然后保存整个json对象
import json
with open('data.json','r',encoding='utf8') as fr:
data = json.loads(fr.read())
data_list = data["data"]["list"]
for lst in data_list:
print(f"区服:{lst['gameAccountQufu']}")
attr_value_list = lst["attrValueList"]
for attr_value in attr_value_list:
if "productAttributeId" in attr_value:
print(f'productAttributeId:{attr_value["productAttributeId"]}')
print(f'name:{attr_value["name"]}\tvalue:{attr_value["value"]}')
# 写入数据
attr_value["114514"] = 114514
print("-"*50)
with open('data2.json','w',encoding='utf8') as fw:
# 保存修改后的json数据
fw.write(json.dump(data))
xiaojipkhuang1 发表于 2024-8-16 14:43
大佬,再请教下,假如我要把这些数据放到json里面,然后根据对应的productAttributeId去获取对应的值赋值 ...
忘记点回复了,具体看评论吧 WXjzc 发表于 2024-8-16 14:49
直接找到对应的对象,然后obj["你要新增或修改的字段"]=对应的值,就可以,然后保存整个json对象
好的,谢谢我看看 WXjzc 发表于 2024-8-16 14:49
直接找到对应的对象,然后obj["你要新增或修改的字段"]=对应的值,就可以,然后保存整个json对象
{'id': 17040446, 'productAttributeId': 126, 'value': '进度号专区', 'intValue': -1, 'values': ['进度号专区'], 'type': 3, 'name': '账号专区', 'ename': '', 'selectType': 0, 'inputType': 0, 'filterType': 0, 'searchType': 0, 'relatedStatus': 0, 'handAddStatus': 0, 'searchSort': 0}
{'id': 17569774, 'productAttributeId': 873, 'value': '1', 'intValue': 1, 'values': ['1'], 'type': 1, 'name': '未使用天赏石', 'ename': None, 'selectType': 0, 'inputType': 0, 'filterType': 0, 'searchType': 0, 'relatedStatus': 0, 'handAddStatus': 0, 'searchSort': 8}
{'id': 17569775, 'productAttributeId': 852, 'value': '未知', 'intValue': -1, 'values': ['未知'], 'type': 1, 'name': '换绑CD', 'ename': None, 'selectType': 0, 'inputType': 0, 'filterType': 0, 'searchType': 0, 'relatedStatus': 0, 'handAddStatus': 0, 'searchSort': 0}
大佬,请教下,我整理了一下,下一步如何对不同的productAttributeId获取对应的value值呢
页:
[1]
2