python处理json数据
找出以下json数据中children键对应列表为空的data键的id值。
例如以下示例children键对应的列表为空时,返回data键的id值:
1 | { |
- 方法:
分析数据可知,该文本数组为字典类型的嵌套,故可以使用递归的方法解决。
首先全部输入文本就是一个大字典,因此递归函数输入为一个大字典,然后判断字典中children键对应的列表是否为空,
如果为空则将children键对应的data键的id加入结果列表,如果children键对应的列表(列表中的元素仍为字典)不为空,
则遍历列表中的字典,继续使用该递归函数进行处理,依次类推。 - 重要的一步
在处理json数据中极为重要的一步是将一个字符串表示的字典转换为python中的字典。
我们可以使用python中的内置模块json中的loads方法将一个字符串表示的字典转换为json数据,既python中的字典。1
json_data = json.loads(string_dict)
- 输入:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223{
"data": {
"created": 1655369372565,
"text": "编程0615版本",
"id": "0o246ba3ptq3cl2nk4fvaq2fcr"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "“录制视频作品”能力升级",
"id": "46bjepg30ird0cion555r1ed13"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "作品H5页面",
"id": "6i25gn6ps4spre3g4q6l02k0vg"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "项目分享",
"id": "b439a445-1e0e-45f1-80ef-9708e8aa5696"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "不变",
"id": "2d1d9bc0-deaf-403c-9cbb-fc018f12bd4d"
},
"children": []
}
]
},
{
"data": {
"created": 1655369372565,
"text": "视频分享",
"id": "6f90564f-2102-4a00-95e8-c16a0ce69569"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "页面下方增加试试运行程序按钮",
"id": "6e909a32-80cb-4a5d-8c11-b82ca39ef651"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "点击按钮跳转项目分享页面",
"id": "a30a997b-5531-4f7b-8fc8-dd7fd0b1446e"
},
"children": []
},
{
"data": {
"created": 1655369372565,
"text": "题型验证",
"id": "02a44ad1-95d1-4901-9345-10f4f8347b97"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "C++题目",
"id": "40a0b6f1-c7d2-4b18-ae0b-aef1237e51db"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "有",
"id": "9bffb81a-a4f6-441f-a6b0-6f13b86e7ccc"
},
"children": []
}
]
},
{
"data": {
"created": 1655369372565,
"text": "python普通",
"id": "e88f0fd1-120c-4867-bd89-d9d066d36ec9"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "有",
"id": "57795e2a-19bc-4992-9c38-81e8333f7ede"
},
"children": []
}
]
},
{
"data": {
"created": 1655369372565,
"text": "python超级",
"id": "0905ce99-0c95-4c26-ba4e-46f1d3a8eeb6"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "没有",
"id": "b5b60820-5ace-4e23-9acc-4be2e9a0c2b8"
},
"children": []
}
]
},
{
"data": {
"created": 1655369372565,
"text": "前端",
"id": "7fbbb053-0237-4e7e-8fac-868819efb651"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "没有",
"id": "a2c9b1e0-11a8-4847-965d-169823e979f4"
},
"children": []
}
]
},
{
"data": {
"created": 1655369372565,
"text": "Scratch题",
"id": "0fdd6097-740f-4ea3-a591-27bdf8295d1c"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "有",
"id": "0a2df200-f11a-49da-9f3c-d3f093d78b84"
},
"children": []
}
]
},
{
"data": {
"created": 1655369372565,
"text": "Scratch简化题",
"id": "01b03c7a-9a4c-4b74-aa6e-d4985010ffcd"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "有",
"id": "081ad0cb-5df8-4bea-ad9e-a787c0c4f90f"
},
"children": []
}
]
},
{
"data": {
"created": 1655369372565,
"text": "CodeMonkey题",
"id": "fee1debf-75b5-40e1-b3ae-0c4657d2dada"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "没有",
"id": "c957f9cc-d547-4f84-8e01-1b5e51f433c1"
},
"children": []
}
]
}
]
},
{
"data": {
"created": 1655369372565,
"text": "位置验证",
"id": "d801a744-7680-456d-a5c7-43a06a05662a"
},
"children": [
{
"data": {
"created": 1655369372565,
"text": "非在线课堂端",
"id": "8174ef9d-b9c2-4e83-ba08-02bda754d8eb"
},
"children": []
},
{
"data": {
"created": 1655369372565,
"text": "社区",
"id": "460baee8-8c50-4fd9-a108-6f12dc375b5d"
},
"children": []
}
]
}
]
}
]
}
]
}
]
}
]
} - 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25import json
import sys
input_dict = ""
for line in sys.stdin:
input_dict += line
if line[0] == '}':
break
print(input_dict)
json_data = json.loads(input_dict)
result = []
def recursion(json_data):
curr_data = json_data['data']
curr_children = json_data['children']
if len(curr_children) > 0:
for next_level_children in curr_children:
recursion(next_level_children)
else:
result.append(curr_data['id'])
return
recursion(json_data)
print(result) - 输出
1
['2d1d9bc0-deaf-403c-9cbb-fc018f12bd4d', 'a30a997b-5531-4f7b-8fc8-dd7fd0b1446e', '9bffb81a-a4f6-441f-a6b0-6f13b86e7ccc', '57795e2a-19bc-4992-9c38-81e8333f7ede', 'b5b60820-5ace-4e23-9acc-4be2e9a0c2b8', 'a2c9b1e0-11a8-4847-965d-169823e979f4', '0a2df200-f11a-49da-9f3c-d3f093d78b84', '081ad0cb-5df8-4bea-ad9e-a787c0c4f90f', 'c957f9cc-d547-4f84-8e01-1b5e51f433c1', '8174ef9d-b9c2-4e83-ba08-02bda754d8eb', '460baee8-8c50-4fd9-a108-6f12dc375b5d']