我有一个带有以下示例json条目的json文件:
{
"title": "Test prod",
"leafPage": true,
"type": "product",
"product": {
"title": "test product",
"offerPrice": "$19.95",
"offerPriceDetails": {
"amount": 19.95,
"text": "$19.95",
"symbol": "$"
},
"media": [
{
"link": "http://www.test.com/cool.jpg",
"primary": true,
"type": "image",
"xpath": "/html[1]/body[1]/div[1]/div[3]/div[2]/div[1]/div[1]/div[1]/div[1]/a[1]/img[1]"
}
],
"availability": true
},
"human_language": "en",
"url": "http://www.test.com"
}
当我使用时,我可以通过python脚本将其发送到我的测试服务器:
"text": entry.get("title"),
"url": entry.get("url"),
"type": entry.get("type"),
但是我无法获取以下嵌套项来上传值,如何构造python json调用以获取嵌套的python json条目?
我已经尝试了以下但没有成功,我需要将它作为.get,因为json文件中当前有不同的字段,并且没有.get调用时出错。
"Amount": entry.get("product"("offerPrice"))
非常感谢有关如何构建嵌套json条目的任何帮助。
答案 0 :(得分:5)
你需要这样做:
"Amount": entry.get("product", {}).get("offerPrice")
entry.get("product", {})
返回产品字典(如果没有product
密钥,则返回空字典)。