复杂Python词典项检索/打印

2024-09-27 04:19:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现自己陷入了python字典(json)的地狱。请看下面我模拟的json代码,它模仿了我真实数据的一小部分。我了解如何使用Python通过jsondata['Dict1]['Dict1a']['Key2'][0]['Key1a'][0]['Key1b']['Key12c']获取与Key1eKey2e关联的值。但现在我想在每次出现时打印这些值。编辑我使用的是json模块。你知道吗

编辑:我尝试过:

activity = jsondata['Dict1']['Dict1a']['Key2'][0]['Key1a']

…然后我想迭代其中的15项来打印,所以我尝试:

for i in activity:
    print ['Key1b']['Key12c'] #to actually retrieve what's in `Key12c`

我也试过:

holder = []
for i in activity:
    holder.append(['Key1b']['Key12c'])

…没有用(当然是同样的错误)

但上面产生了“列表索引必须是整数,而不是str”的错误。我应该尝试获取索引值吗?你知道吗

我还提供了一个将代码粘贴到在线JSON查看器中时显示的JSON结构截图。你知道吗

{"Dict1":{"Dict1a":{"Key1":"Value1","Key2":[{"Key1a":[{"Key1b":
{"Key1c":"Value1c","Key2c":"Value2c","Key3c":{"Key1d":"Value1d","Key2d":"Value2d"},
"Key4c":"Value4c","Key5c":"Value5c","Key6c":"Value6c","Key7c":"Value7c",
"Key8c":"Value8c","Key9c":"Value9c","Key10c":[],"Key11c":"Value11c",
"Key12c":{"Key1e": 1234,"Key2e": 5378}}}]}]}}}

另一个编辑:请注意,Key1a下的0表示15个中的一个,但在下面的JSON视图中没有表示。我想重复这15条。你知道吗

This is the JSON structure (I pasted the above code into an online json viewer)


Tags: 代码injson编辑activitykey2jsondatadict1

热门问题