跳到正文

题解

题解:简易 JSON 解析

Folden_xiaoming约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月20日
篇幅
约 1 分钟
题目
B4332

版本

  1. v1当前

看到题目的我突然想起了什么……

python大法好!

先上代码

Python
import json
a=input()
d=json.loads(a)
p=int(input())
temp=input()
temp2=d[temp]
for i in range(1,p):
    temp=input()
    temp2=temp2[temp]
print(temp2)

我们来一行一行分析:

第一行:

Python
 import json

导入 json 库,用于后面字典类型强制转换。

第二行:

Python
a=input()

输入 a 即题目中的 json 文本。

第三行:

Python
d=json.loads(a)

将字符串 a 转换成 python 中的字典类型,并存储到 d 中。

第四行:

Python
p=int(input())

输入 p,含义rt。

第五,六行:

Python
temp=input()
temp2=d[temp]

输入第一个键值,并存储到 temp 中,并将 temp 作为键值查找出字典中的值(或者是嵌套的下一层字典)

第七,八,九行:

Python
for i in range(1,p):
    temp=input()
    temp2=temp2[temp]

利用循环查找嵌套字典

第十行:

Python
print(temp2)

这时的 temp2 就是答案了

用 python 十行就能写完这道题,是不是很便捷呢?

蒟蒻第一篇题解,求过

原文