不知道如何访问JSON代码中的嵌套值?
创始人
2025-01-12 03:31:10
0

要访问JSON代码中的嵌套值,可以使用点号(.)或方括号([])来访问对象属性或数组元素。

下面是一个JSON代码的示例:

{
  "name": "John",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "New York"
  },
  "hobbies": ["reading", "running", "painting"]
}

以下是几种访问嵌套值的方法:

  1. 使用点号(.)来访问对象属性:
import json

data = '''
{
  "name": "John",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "New York"
  },
  "hobbies": ["reading", "running", "painting"]
}
'''

json_data = json.loads(data)

name = json_data["name"]
street = json_data["address"]["street"]
city = json_data["address"]["city"]

print(name)    # 输出: John
print(street)  # 输出: 123 Main St
print(city)    # 输出: New York
  1. 使用方括号([])来访问对象属性或数组元素:
import json

data = '''
{
  "name": "John",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "New York"
  },
  "hobbies": ["reading", "running", "painting"]
}
'''

json_data = json.loads(data)

name = json_data["name"]
street = json_data["address"]["street"]
city = json_data["address"]["city"]
hobby = json_data["hobbies"][0]

print(name)    # 输出: John
print(street)  # 输出: 123 Main St
print(city)    # 输出: New York
print(hobby)   # 输出: reading

无论是使用点号还是方括号,都可以访问JSON代码中的嵌套值。

相关内容

热门资讯

透视安装!hhpoker免费透... 透视安装!hhpoker免费透视脚本,aapoker可以开挂吗,重大来袭(有挂透明)1、构建自己的微...
透视线上!hhpoker透视脚... 透视线上!hhpoker透视脚本,拱趴大菠萝万能辅助器,可靠技巧(都是真的是有挂)准备好在hhpok...
透视线上!红龙poker透视,... 1、透视线上!红龙poker透视,德扑圈透视,细节方法(往昔有挂);详细教程。2、红龙poker透视...
透视透视!红龙poker辅助,... 透视透视!红龙poker辅助,aapoker有脚本吗,实测交流(有挂教程)是一款可以让一直输的玩家,...
透视挂!wepoker底牌透视... 透视挂!wepoker底牌透视脚本下载,hhpoker到底可以作弊吗,必赢教程(起初真的有挂);支持...
透视app!aapoker辅助... 透视app!aapoker辅助插件工具下载,hhpoker辅助下载,专业讨论(有挂方法)1、每一步都...
透视真的!hhpoker辅助软... 透视真的!hhpoker辅助软件,poker红龙辅助,辅助教程(竟然存在有挂);原来确实真的有挂(需...
透视安装!hhpoker辅助软... 透视安装!hhpoker辅助软件,wepoker底牌透视脚本http,揭秘真相(有挂总结);wepo...
透视好友房!aapoker辅助... 透视好友房!aapoker辅助工具存在吗,wpk德州局怎么透视,揭秘教程(一向是有挂);是一款可以让...
透视模拟器!hhpoker辅助... 透视模拟器!hhpoker辅助软件,hhpoker透视脚本安卓,分享给玩家(有挂攻略)是一款可以让一...