使用2个关键字从json文本中提取正确的id?

时间:2017-08-30 18:13:36

标签: python json

对如何正确格式化此代码以指定正确的颜色和大小感到困惑。我的目标是获得id颜色Black和尺寸Medium。我想要提取的输出是46779。我如何设置代码以获得我想要拉出的输出?

代码 -

color = "Black"
size_choice = 'Medium'

data1 = response1.text
size_data = json.loads(data1)['styles']
for entry in (size_data):
    if entry['name'] == color['sizes']:
        if entry['name'] == size_choice:
            size_id = (entry['id'])
            print("size id: ", size_id)

目前代码输出此错误:

    if entry['name'] == color['sizes']:
TypeError: string indices must be integers

Json -

{"styles":[{"id":17289,"name":"Black","currency":"USD","description":null,"image_url":"//d17ol771963kd3.cloudfront.net/137366/sm/Am0dyTgmEJg.jpg","image_url_hi":"//d17ol771963kd3.cloudfront.net/137366/rs/Am0dyTgmEJg.jpg","swatch_url":"//d17ol771963kd3.cloudfront.net/137366/ca/Am0dyTgmEJg.jpg","swatch_url_hi":"//d17ol771963kd3.cloudfront.net/137366/rc/Am0dyTgmEJg.jpg","mobile_zoomed_url":"//d17ol771963kd3.cloudfront.net/137366/mo/Am0dyTgmEJg.jpg","mobile_zoomed_url_hi":"//d17ol771963kd3.cloudfront.net/137366/rm/Am0dyTgmEJg.jpg","bigger_zoomed_url":"//d17ol771963kd3.cloudfront.net/137366/zo/Am0dyTgmEJg.jpg","sizes":[{"name":"Small","id":46778,"stock_level":0},{"name":"Medium","id":46779,"stock_level":0},{"name":"Large","id":46780,"stock_level":0},{"name":"XLarge","id":46781,"stock_level":0}],"additional":[]},{"id":17290,"name":"White","currency":"USD","description":null,"image_url":"//d17ol771963kd3.cloudfront.net/137372/sm/tjvfNXfNMtY.jpg","image_url_hi":"//d17ol771963kd3.cloudfront.net/137372/rs/tjvfNXfNMtY.jpg","swatch_url":"//d17ol771963kd3.cloudfront.net/137372/ca/tjvfNXfNMtY.jpg","swatch_url_hi":"//d17ol771963kd3.cloudfront.net/137372/rc/tjvfNXfNMtY.jpg","mobile_zoomed_url":"//d17ol771963kd3.cloudfront.net/137372/mo/tjvfNXfNMtY.jpg","mobile_zoomed_url_hi":"//d17ol771963kd3.cloudfront.net/137372/rm/tjvfNXfNMtY.jpg","bigger_zoomed_url":"//d17ol771963kd3.cloudfront.net/137372/zo/tjvfNXfNMtY.jpg","sizes":[{"name":"Small","id":46782,"stock_level":0},{"name":"Medium","id":46783,"stock_level":0},{"name":"Large","id":46784,"stock_level":0},{"name":"XLarge","id":46785,"stock_level":0}],"additional":[]},{"id":17292,"name":"Lime","currency":"USD","description":null,"image_url":"//d17ol771963kd3.cloudfront.net/137369/sm/9JM2v7Uwwi4.jpg","image_url_hi":"//d17ol771963kd3.cloudfront.net/137369/rs/9JM2v7Uwwi4.jpg","swatch_url":"//d17ol771963kd3.cloudfront.net/137369/ca/9JM2v7Uwwi4.jpg","swatch_url_hi":"//d17ol771963kd3.cloudfront.net/137369/rc/9JM2v7Uwwi4.jpg","mobile_zoomed_url":"//d17ol771963kd3.cloudfront.net/137369/mo/9JM2v7Uwwi4.jpg","mobile_zoomed_url_hi":"//d17ol771963kd3.cloudfront.net/137369/rm/9JM2v7Uwwi4.jpg","bigger_zoomed_url":"//d17ol771963kd3.cloudfront.net/137369/zo/9JM2v7Uwwi4.jpg","sizes":[{"name":"Small","id":46790,"stock_level":0},{"name":"Medium","id":46791,"stock_level":0},{"name":"Large","id":46792,"stock_level":0},{"name":"XLarge","id":46793,"stock_level":0}],"additional":[]},{"id":17377,"name":"Heather Grey","currency":"USD","description":null,"image_url":"//d17ol771963kd3.cloudfront.net/137368/sm/eu2N2hb3yHY.jpg","image_url_hi":"//d17ol771963kd3.cloudfront.net/137368/rs/eu2N2hb3yHY.jpg","swatch_url":"//d17ol771963kd3.cloudfront.net/137368/ca/eu2N2hb3yHY.jpg","swatch_url_hi":"//d17ol771963kd3.cloudfront.net/137368/rc/eu2N2hb3yHY.jpg","mobile_zoomed_url":"//d17ol771963kd3.cloudfront.net/137368/mo/eu2N2hb3yHY.jpg","mobile_zoomed_url_hi":"//d17ol771963kd3.cloudfront.net/137368/rm/eu2N2hb3yHY.jpg","bigger_zoomed_url":"//d17ol771963kd3.cloudfront.net/137368/zo/eu2N2hb3yHY.jpg","sizes":[{"name":"Small","id":47110,"stock_level":0},{"name":"Medium","id":47111,"stock_level":0},{"name":"Large","id":47112,"stock_level":0},{"name":"XLarge","id":47113,"stock_level":0}],"additional":[]},{"id":17293,"name":"Brown","currency":"USD","description":null,"image_url":"//d17ol771963kd3.cloudfront.net/137367/sm/owzelzda9e0.jpg","image_url_hi":"//d17ol771963kd3.cloudfront.net/137367/rs/owzelzda9e0.jpg","swatch_url":"//d17ol771963kd3.cloudfront.net/137367/ca/owzelzda9e0.jpg","swatch_url_hi":"//d17ol771963kd3.cloudfront.net/137367/rc/owzelzda9e0.jpg","mobile_zoomed_url":"//d17ol771963kd3.cloudfront.net/137367/mo/owzelzda9e0.jpg","mobile_zoomed_url_hi":"//d17ol771963kd3.cloudfront.net/137367/rm/owzelzda9e0.jpg","bigger_zoomed_url":"//d17ol771963kd3.cloudfront.net/137367/zo/owzelzda9e0.jpg","sizes":[{"name":"Small","id":46794,"stock_level":0},{"name":"Medium","id":46795,"stock_level":0},{"name":"Large","id":46796,"stock_level":0},{"name":"XLarge","id":46797,"stock_level":0}],"additional":[]},{"id":17294,"name":"Pale Blue","currency":"USD","description":null,"image_url":"//d17ol771963kd3.cloudfront.net/138342/sm/Wdv-CY_QC7Y.jpg","image_url_hi":"//d17ol771963kd3.cloudfront.net/138342/rs/Wdv-CY_QC7Y.jpg","swatch_url":"//d17ol771963kd3.cloudfront.net/138342/ca/Wdv-CY_QC7Y.jpg","swatch_url_hi":"//d17ol771963kd3.cloudfront.net/138342/rc/Wdv-CY_QC7Y.jpg","mobile_zoomed_url":"//d17ol771963kd3.cloudfront.net/138342/mo/Wdv-CY_QC7Y.jpg","mobile_zoomed_url_hi":"//d17ol771963kd3.cloudfront.net/138342/rm/Wdv-CY_QC7Y.jpg","bigger_zoomed_url":"//d17ol771963kd3.cloudfront.net/138342/zo/Wdv-CY_QC7Y.jpg","sizes":[{"name":"Small","id":46798,"stock_level":0},{"name":"Medium","id":46799,"stock_level":0},{"name":"Large","id":46800,"stock_level":0},{"name":"XLarge","id":46801,"stock_level":0}],"additional":[]},{"id":17295,"name":"Red","currency":"USD","description":null,"image_url":"//d17ol771963kd3.cloudfront.net/137371/sm/-cCSOap0j1M.jpg","image_url_hi":"//d17ol771963kd3.cloudfront.net/137371/rs/-cCSOap0j1M.jpg","swatch_url":"//d17ol771963kd3.cloudfront.net/137371/ca/-cCSOap0j1M.jpg","swatch_url_hi":"//d17ol771963kd3.cloudfront.net/137371/rc/-cCSOap0j1M.jpg","mobile_zoomed_url":"//d17ol771963kd3.cloudfront.net/137371/mo/-cCSOap0j1M.jpg","mobile_zoomed_url_hi":"//d17ol771963kd3.cloudfront.net/137371/rm/-cCSOap0j1M.jpg","bigger_zoomed_url":"//d17ol771963kd3.cloudfront.net/137371/zo/-cCSOap0j1M.jpg","sizes":[{"name":"Small","id":46802,"stock_level":0},{"name":"Medium","id":46803,"stock_level":0},{"name":"Large","id":46804,"stock_level":0},{"name":"XLarge","id":46805,"stock_level":0}],"additional":[]}],"description":"All cotton classic Supreme t-shirt with printed graphic on front. \u003cspan class=\"red\"\u003e*T-shirts fit larger than previous styles. Please check sizing for updated measurements.\u003c/span\u003e","can_add_styles":false,"can_buy_multiple":false,"ino":"FW17T15","cod_blocked":false,"canada_blocked":false,"purchasable_qty":1,"new_item":false,"apparel":true,"handling":0,"no_free_shipping":false,"can_buy_multiple_with_limit":0}

1 个答案:

答案 0 :(得分:1)

错误位于T:您已声明T,因此您无法像这样查找密钥color['sizes']

您需要在循环中添加另一个级别来搜索大小:

color = "Black"