python - 如何在 Python 中处理这个 JSON 文件?
问题描述
我正在尝试从以下 JSON 文件中提取“点”部分,但发生错误:
with open("file1.json", "r") as read_file:
data = json.load(read_file)
data['points']
这是我的错误:
KeyError
Traceback (most recent call last)
<ipython-input-127-a97424859b31> in <module>()
----> 1 data['points']
KeyError: 'points'
这是我的 JSON:
{
"duration": 2727,
"height": 756,
"periods": [
{
"actions": [
{
"action": "createShape",
"actionID": 217,
"endTime": 2727,
"points": [
{
"t": 2014,
"x": 715,
"y": 367.5
},
{
"t": 2049,
"x": 714.5,
"y": 367.5
},
{
"t": 2064,
"x": 713.5,
"y": 367.5
},
{
"t": 2096,
"x": 711,
"y": 367.5
},
{
"t": 2115,
"x": 707.5,
"y": 368
},
{
"t": 2132,
"x": 705,
"y": 368
},
{
"t": 2148,
"x": 702,
"y": 368.5
},
{
"t": 2162,
"x": 700,
"y": 368.5
},
{
"t": 2176,
"x": 697,
"y": 369
},
{
"t": 2198,
"x": 693,
"y": 369
},
{
"t": 2215,
"x": 690,
"y": 369
},
{
"t": 2231,
"x": 686,
"y": 368.5
},
{
"t": 2248,
"x": 681,
"y": 368.5
},
{
"t": 2265,
"x": 676.5,
"y": 368.5
},
{
"t": 2276,
"x": 674.5,
"y": 368.5
},
{
"t": 2298,
"x": 671,
"y": 369
},
{
"t": 2315,
"x": 667.5,
"y": 370
},
{
"t": 2333,
"x": 664.5,
"y": 370
},
{
"t": 2348,
"x": 660.5,
"y": 370
},
{
"t": 2365,
"x": 656.5,
"y": 370
},
{
"t": 2382,
"x": 653,
"y": 370.5
},
{
"t": 2399,
"x": 650,
"y": 370.5
},
{
"t": 2415,
"x": 647.5,
"y": 370.5
},
{
"t": 2432,
"x": 644,
"y": 370.5
},
{
"t": 2449,
"x": 640.5,
"y": 370.5
},
{
"t": 2460,
"x": 638.5,
"y": 370.5
},
{
"t": 2478,
"x": 635,
"y": 370
},
{
"t": 2498,
"x": 630,
"y": 370
},
{
"t": 2515,
"x": 628,
"y": 370
},
{
"t": 2531,
"x": 625.5,
"y": 370
},
{
"t": 2549,
"x": 623,
"y": 370
},
{
"t": 2565,
"x": 620,
"y": 370
},
{
"t": 2584,
"x": 618,
"y": 370
},
{
"t": 2594,
"x": 617,
"y": 370
},
{
"t": 2609,
"x": 615,
"y": 370
},
{
"t": 2626,
"x": 613.5,
"y": 370
},
{
"t": 2643,
"x": 612.5,
"y": 370
},
{
"t": 2697,
"x": 614.5,
"y": 370
},
{
"t": 2705,
"x": 615,
"y": 370
},
{
"t": 2727,
"x": 616,
"y": 370.5
}
],
"startTime": 2014,
"strokeColor": "#F8A602",
"strokeWidth": 4,
"type": "pen"
}
],
"endTime": 2727,
"id": 1,
"startTime": 0,
"type": "recording"
}
],
"platform": "iOS",
"width": 1344
}
但它仅适用于“期间”部分!
任何人都可以提供一些帮助吗?
解决方案
对于提供的 JSON 文件,我们可以提取点部分如下:
import json
with open("file1.json", "r") as read_file:
data = json.load(read_file)
points = data['periods'][0]['actions'][0]['points']
让我们看看我们是如何一步一步实现这一目标的:
# get periods section
periods_lst = data['periods']
# periods is a list to drill down into the data structure
# we have to specify the element of that list by index
# that list contain only 1 element with index 0
periods = periods_lst[0]
# get actions section
actions_lst = periods['actions']
# actions is a list which also contains only 1 element
actions = actions_lst[0]
# get points
points_lst = actions['points']
# get necessary point by index, e.g.
point0 = points_lst[0]
point8 = points_lst[8]
# get the list of points in one line
points = data['periods'][0]['actions'][0]['points']
推荐阅读
- three.js - DeckGL 在海量点云数据可视化方面的表现如何?
- angular - 监听子组件中的更改变量
- mysql - 在 Google Cloud SQL 上更改 innodb_io_capacity
- ios - 如何在 iOS 中检查暗模式?
- java - 如何在没有 numpy 的情况下将 Python 代码转换为 Java
- react-native - 如何在 Linux 上手动启动 React Native 调试器 - Ubuntu
- android - 错误:迁移到 androidx 后找不到符号类 MediaStyle
- flutter - 如何使所有列表视图同时可滚动?
- android - 如何使用 MVVM 架构实现 Firebase Google SignIn?
- swift - 在外面点击时如何关闭我的幻灯片菜单?