在Python中解析Json并找到一个值

时间:2019-05-19 12:20:57

标签: python json parsing

我正在尝试使用Python和json库解析以下Json

{"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 %>\u00a0 \u20ac","currencyFormat":"%s\u00a0 \u20ac","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\u00a0 \u20ac","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}}}

我想获得特定鞋类尺寸的“产品”价值,例如。如果我要42,请找到78835。

我试图通过两种方式做到这一点:

1。

tex=THEJSONTEXT
jsn=json.loads(tex)
jsn['attributes']['173'].get("products","")

但这对我不起作用。 所以我尝试了版本2:

import re
prod=re.findall('"Footwear-42","products":["\d\d\d\d\d"]', tex)

即使这对我也不起作用。...

2 个答案:

答案 0 :(得分:1)

实际上,还有一层嵌套。请尝试以下。

for i in jsn['attributes']['173']["options"]:
    print(i["products"])

options也是一个列表。

答案 1 :(得分:1)

module example.com/me/hello

require (
  github.com/me/some-repo v0.0.0
)

replace (
    github.com/augustoroman/v8 => github.com/me/v8
)

您可以看到产品不存在,这就是代码无法正常工作的原因。您正在正确解析JSON。

稍微格式化JSON

>>> jsn['attributes']['173'].keys()
dict_keys(['id', 'code', 'label', 'options', 'position'])

向下钻取JSON变得容易一些。我看到的与尺寸有关的唯一属性是各个选项的{ "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" ] }, ... much more 键。那是对的吗?如果是这样,则必须筛选键,然后label列表才能执行所需的操作。

所以..想象这样分配选项列表:

options

然后,您可以像这样过滤列表到所需的产品:

options = obj['attributes']['173']['options']

那么>>> size = 42 >>> list(filter(lambda x: x['label'][-1 * len(str(size)):] == str(size), options)) [{'id': '355', 'label': 'Footwear-42', 'products': ['78835']}] 的作用是什么?这是一个lambda,可以有效地做到这一点:

lambda x: x['label'][-1 * len(str(size)):]

我根据float / int的大小查看标签的最后size = 42 def filter_product(product_object): label = product_object['label'] product_size = [-1 * len(str(size)):] # get the last digits of the str that are the size if str(size) == product_size: return True else: return False 位数字。然后,我将其与您想要的尺寸进行比较,如果相同,那么这就是您要寻找的产品。