我觉得我很亲近,但我无法解决这个问题。我只想打印字段tpnc的数值结果。只有一个数字要打印。在
我的代码是:import httplib, urllib, base64, json, requests
headers = {
# Request headers
'Ocp-Apim-Subscription-Key': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
}
barcode = input("Enter a barcode:")
params = urllib.urlencode({
# Request parameters
'gtin': str(barcode),
'tpnb': '{string}',
'tpnc': '{string}',
'catid': '{string}',
})
#try:
conn = httplib.HTTPSConnection('dev.tescolabs.com')
conn.request("GET", "/product/?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
json = json.loads(data)
print json ["products"]['tpnc']
以下是我收到的错误:
^{pr2}$
在我看来,产品是关键,然后是tpnc,但这可能是我弄错的地方。这是返回的原始Json(在下面的第6行)。在{
"products": [
{
"gtin": "05057373842824",
"tpnb": "059266919",
"tpnc": "297243995",
"description": "Tesco Light Mayonnaise 450Ml",
"brand": "TESCO",
"qtyContents": {
"quantity": 450.0,
"totalQuantity": 450.0,
"quantityUom": "ml",
"drainedWeight": "-;-",
"netContents": "450ml e (PET);650ml e (PET)"
},
"productCharacteristics": {
"isFood": true,
"isDrink": false,
"healthScore": 50,
"isHazardous": false,
"storageType": "Ambient",
"isNonLiquidAnalgesic": false,
"containsLoperamide": false
},
"ingredients": [
"Water",
"Rapeseed Oil (28%), Sugar, Modified Maize Starch, Spirit Vinegar, Free Range Pasteurised Egg Yolk (4%), Potassium Chloride, Lemon Juice From Concentrate, Salt, Preservative (Sorbic Acid), Stabilisers (Xanthan Gum, Guar Gum), Mustard Seed, Acidity Regulator (Citric Acid), Turmeric Powder, Antioxidant (Rosemary Extract).
"
],
"gda": {
"gdaRefs": [
{
"gdaDescription": "Guideline Amounts Per Serv",
"headers": [
"Each tablespoon contains"
],
"footers": [
"*Reference intake of an average adult (8400 kJ / 2000 kcal)",
"Typical values per 100ml: Energy 1271.56kJ (307.72kcal)"
],
"values": [
{
"name": "Energy",
"values": [
"191kJ",
"46kcal"
],
"percent": "2"
},
{
"name": "Fat",
"values": [
"4.4g"
],
"percent": "6",
"rating": "high"
},
{
"name": "Saturates",
"values": [
"0.4g"
],
"percent": "2",
"rating": "medium"
},
{
"name": "Sugars",
"values": [
"0.8g"
],
"percent": "1",
"rating": "medium"
},
{
"name": "Salt",
"values": [
"0.1g"
],
"percent": "2",
"rating": "medium"
}
]
}
]
},
"calcNutrition": {
"per100Header": "100ml contains",
"perServingHeader": "Each tablespoon (15ml) contains",
"calcNutrients": [
{
"name": "Energy (kJ)",
"valuePer100": "1271.56",
"valuePerServing": "190.73"
},
{
"name": "Energy (kcal)",
"valuePer100": "307.72",
"valuePerServing": "46.16"
},
{
"name": "Fat (g)",
"valuePer100": "29",
"valuePerServing": "4.35"
},
{
"name": "Saturates (g)",
"valuePer100": "2.5",
"valuePerServing": ".38"
},
{
"name": "Carbohydrate (g)",
"valuePer100": "11.1",
"valuePerServing": "1.67"
},
{
"name": "Sugars (g)",
"valuePer100": "5.23",
"valuePerServing": ".78"
},
{
"name": "Fibre"
},
{
"name": "Protein (g)",
"valuePer100": ".58",
"valuePerServing": ".09"
},
{
"name": "Salt (g)",
"valuePer100": ".97",
"valuePerServing": ".15"
}
]
},
"allergenAdvice": {
"allergenText": "For allergens, see ingredients in bold."
},
"storage": [
"Store in a cool, dry place. Once opened, keep refrigerated and consume within 28 days."
],
"marketingText": "Made with Free Range Eggs",
"pkgDimensions": [
{
"no": 1,
"height": 19.3,
"width": 8.1,
"depth": 5.5,
"dimensionUom": "cm",
"weight": 502.0,
"weightUom": "g",
"volume": 859.815,
"volumeUom": "cc"
}
],
"productAttributes": [
{
"category": [
{
"lifestyle": [
{
"lifestyle": {
"name": "Lifestyle",
"value": "Suitable for Vegetarians"
}
}
]
}
]
}
]
}
]
}