Python解析JSON:如何获取特定鞋码对应的products值
如何用Python解析JSON获取对应鞋码的产品ID
你之前尝试的两种方法都没成功,咱们来逐个解决问题:
方法一:用json库正确解析(推荐)
你之前写的jsn['attributes']['173'].get("products","")无效,是因为**products字段根本不在attributes['173']这个层级**——它藏在attributes['173']['options']数组里的每一个选项对象中。你需要遍历这个数组,找到对应鞋码的选项,再提取产品ID。
下面是可以直接运行的代码:
import json # 把你的JSON文本赋值给tex变量 tex = '''{"attributes":{"173":{"id":"173","code":"Size","label":"Size","options":[{"id":"352","label":"Footwear-41","products":["78834"]},{"id":"355","label":"Footwear-42","products":["78835"]},{"id":"357","label":"Footwear-42.5","products":["78836"]},{"id":"358","label":"Footwear-43","products":["78837"]},{"id":"361","label":"Footwear-44","products":["78838"]},{"id":"363","label":"Footwear-44.5","products":["78839"]},{"id":"364","label":"Footwear-45","products":["78840"]},{"id":"367","label":"Footwear-46","products":["78841"]}],"position":"0"}},"template":"<%- data.price %> €","currencyFormat":"%s €","optionPrices":{"78834":{"oldPrice":{"amount":189.9},"basePrice":{"amount":159.57983093277},"finalPrice":{"amount":189.9},"tierPrices":[]},"78835":{"oldPrice":{"amount":189.9},"basePrice":{"amount":159.57983093277},"finalPrice":{"amount":189.9},"tierPrices":[]},"78836":{"oldPrice":{"amount":189.9},"basePrice":{"amount":159.57983093277},"finalPrice":{"amount":189.9},"tierPrices":[]},"78837":{"oldPrice":{"amount":189.9},"basePrice":{"amount":159.57983093277},"finalPrice":{"amount":189.9},"tierPrices":[]},"78838":{"oldPrice":{"amount":189.9},"basePrice":{"amount":159.57983093277},"finalPrice":{"amount":189.9},"tierPrices":[]},"78839":{"oldPrice":{"amount":189.9},"basePrice":{"amount":159.57983093277},"finalPrice":{"amount":189.9},"tierPrices":[]},"78840":{"oldPrice":{"amount":189.9},"basePrice":{"amount":159.57983093277},"finalPrice":{"amount":189.9},"tierPrices":[]},"78841":{"oldPrice":{"amount":189.9},"basePrice":{"amount":159.57983093277},"finalPrice":{"amount":189.9},"tierPrices":[]}},"priceFormat":{"pattern":"%s €","precision":2,"requiredPrecision":2,"decimalSymbol":",","groupSymbol":".","groupLength":3,"integerRequired":1},"prices":{"oldPrice":{"amount":189.9},"basePrice":{"amount":159.57983093277},"finalPrice":{"amount":189.9}},"productId":"78842","chooseText":"Choose an Option...","images":[],"index":{"78834":{"173":"352"},"78835":{"173":"355"},"78836":{"173":"357"},"78837":{"173":"358"},"78838":{"173":"361"},"78839":{"173":"363"},"78840":{"173":"364"},"78841":{"173":"367"}},"sku":{"default":"CI6400-100","78834":"CI6400-100-Footwear-41","78835":"CI6400-100-Footwear-42","78836":"CI6400-100-Footwear-42.5","78837":"CI6400-100-Footwear-43","78838":"CI6400-100-Footwear-44","78839":"CI6400-100-Footwear-44.5","78840":"CI6400-100-Footwear-45","78841":"CI6400-100-Footwear-46"},"stock":{"78834":{"is_salable":true,"qty":1},"78835":{"is_salable":true,"qty":2},"78836":{"is_salable":true,"qty":3},"78837":{"is_salable":true,"qty":3},"78838":{"is_salable":true,"qty":3},"78839":{"is_salable":true,"qty":1},"78840":{"is_salable":true,"qty":3},"78841":{"is_salable":true,"qty":1}}}''' jsn = json.loads(tex) # 设置你要找的目标鞋码 target_size = "Footwear-42" # 遍历options数组,匹配目标鞋码 for option in jsn['attributes']['173']['options']: if option['label'] == target_size: # products是数组,这里取第一个元素(你的数据里每个选项只有一个产品) product_id = option['products'][0] print(f"鞋码{target_size}对应的产品ID是:{product_id}") break else: # 如果循环没找到匹配项,执行这里 print(f"没找到鞋码{target_size}对应的产品")
运行这段代码,就能输出你要的78835了。
方法二:修正正则表达式(不推荐,仅作参考)
你之前的正则失效,主要是两个问题:
- JSON里
label和products字段之间可能有空格、换行或其他字符,直接硬写连在一起匹配不到; - 转义字符和匹配规则不够灵活。
修正后的正则代码如下:
import re target_size = "Footwear-42" # 用原始字符串构建正则,匹配label为目标鞋码后,提取products里的ID pattern = fr'"label":"{target_size}".*?"products":\[("(\d+)")\]' # re.DOTALL让.匹配换行符 match = re.search(pattern, tex, re.DOTALL) if match: product_id = match.group(2) print(f"鞋码{target_size}对应的产品ID是:{product_id}") else: print(f"没找到鞋码{target_size}对应的产品")
不过还是建议用第一种方法——正则处理JSON太脆弱了,只要JSON结构有一点变化(比如字段顺序调整、多了个空格),正则就会失效,而json库是专门用来解析结构化数据的,靠谱得多。
内容的提问来源于stack exchange,提问作者rocket
相关产品推荐
相关产品推荐

