相关文章推荐
长情的火锅  ·  python读取excel,获得下拉选中所有 ...·  4 周前    · 
勤奋的鸭蛋  ·  python - Set up of ...·  3 周前    · 
大力的长颈鹿  ·  python - Conda env ...·  3 周前    · 
讲道义的闹钟  ·  如何释放Python占用的内存?开发者社区·  5 天前    · 
聪明的橙子  ·  python内存机制与垃圾回收、调优手段 ...·  5 天前    · 
帅气的夕阳  ·  R语言错误的提示(中英文翻译) - 谷子弟 ...·  1 年前    · 
文武双全的豌豆  ·  进击的 Vulkan 移动开发之 ...·  2 年前    · 
旅行中的胡萝卜  ·  Java ...·  2 年前    · 
强健的蛋挞  ·  JavaScript ...·  2 年前    · 
Code  ›  Python .get 嵌套 JSON 值开发者社区
嵌套 python
https://cloud.tencent.com/developer/article/2393476
乖乖的骆驼
3 月前
华科云商小徐

Python .get 嵌套 JSON 值

原创
腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
圈层
工具
MCP广场
文章/答案/技术大牛
发布
首页
学习
活动
专区
圈层
工具
MCP广场
返回腾讯云官网
华科云商小徐
首页
学习
活动
专区
圈层
工具
MCP广场
返回腾讯云官网
社区首页 > 专栏 > Python .get 嵌套 JSON 值

Python .get 嵌套 JSON 值

原创
作者头像
华科云商小徐
发布 于 2024-03-04 11:03:13
发布 于 2024-03-04 11:03:13
750 0 0
代码可运行
举报
文章被收录于专栏: 小徐学爬虫 小徐学爬虫
运行总次数: 0
代码可运行

对于长期使用python写代码的我来说,经常在Python代码中,使用 .get 方法来访问嵌套在JSON结构中的值。我们知道JSON(JavaScript Object Notation)是一种常见的数据交换格式,它可以包含嵌套的键值对。但是在我们使用总该如何获取嵌套对象中的值呢?

1、问题背景

在 Python 中,可以使用 .get() 方法从 JSON 对象中获取值。当 JSON 对象中嵌套了其他 JSON 对象时,如何获取嵌套对象中的值呢?

例如,以下 JSON 对象中包含了一个名为 "product" 的嵌套对象,该对象又包含了几个子对象。

代码语言: javascript
代码 运行次数: 0
运行
复制
{
            "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"
        }

如果要获取 "product" 对象中的 "offerPrice" 值,可以使用以下代码:

代码语言: javascript
代码 运行次数: 0
运行
复制
entry.get("product").get("offerPrice")

这样就可以获取到 "offerPrice" 的值 "$19.95"。

2、解决方案

但是,如果 JSON 对象中的嵌套对象不是直接使用键值对表示,而是使用数组表示,则获取嵌套对象中的值就会变得更加复杂。

例如,以下 JSON 对象中包含了一个名为 "media" 的嵌套数组,该数组中包含了多个子对象。

代码语言: javascript
代码 运行次数: 0
运行
复制
{
    "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]"
                "link": "http://www.test.com/cool2.jpg",
                "primary": false,
                "type": "image",
                "xpath": "/html[1]/body[1]/div[1]/div[3]/div[2]/div[1]/div[1]/div[1]/div[1]/a[2]/img[1]"
        "availability": true
    "human_language": "en",
    "url": "http://www.test.com"
}

如果要获取 "media" 数组中的第一个子对象中的 "link" 值,可以使用以下代码:

代码语言: javascript
代码 运行次数: 0
运行
复制
entry.get("product", {}).get("media", [])[0].get("link")

这样就可以获取到第一个子对象的 "link" 值 " http://www.test.com/cool.jpg "。

代码示例

代码语言: javascript
代码 运行次数: 0
运行
复制
import json
# 读取 JSON 文件
with open('data.json', 'r') as f:
    data = json.load(f)
# 获取 "product" 对象中的 "offerPrice" 值
offer_price = data.get("product", {}).get("offerPrice")
 
推荐文章
长情的火锅  ·  python读取excel,获得下拉选中所有选项_python获取下拉菜单内容 excel
4 周前
勤奋的鸭蛋  ·  python - Set up of virtual environment in anaconda failing - Stack Overflow
3 周前
大力的长颈鹿  ·  python - Conda env create from .yml gives "unexpected error" - Stack Overflow
3 周前
讲道义的闹钟  ·  如何释放Python占用的内存?开发者社区
5 天前
聪明的橙子  ·  python内存机制与垃圾回收、调优手段 - 长安223
5 天前
帅气的夕阳  ·  R语言错误的提示(中英文翻译) - 谷子弟 - 博客园
1 年前
文武双全的豌豆  ·  进击的 Vulkan 移动开发之 Instance & Device & Queue-阿里云开发者社区
2 年前
旅行中的胡萝卜  ·  Java Blob类型和String类型相互转换_java blob转换成string_清风掠影_XQ的博客-CSDN博客
2 年前
强健的蛋挞  ·  JavaScript 将死?_码小辫的技术博客_51CTO博客
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号