CCF CSP 201709-3 JSON查询 python
程序员文章站
2024-03-17 21:34:40
...
import json
n,m = map(int,input().split())
json_str=""
for i in range(n):
json_str += input()
data = json.loads(json_str)
"""
type(data) : dict
{'firstName': 'John', 'lastName': 'Smith', 'address': {'streetAddress': '2ndStreet', 'city': 'NewYork', 'state': 'NY'}, 'esc\\aped': '"hello"'}
print(type(data['firstName'])) # <class 'str'>
print(type(data['address'])) # <class 'dict'>
print(type(data['address']['city'])) # <class 'str'>
"""
querys = []
for i in range(m):
querys.append(input().split("."))
json_type={
str:"STRING",
dict:"OBJECT"
}
for q in querys:
s = ["data"]
try:
# repr(x) 返回一个对象的 string 格式。
for x in q:
s.append("["+repr(x)+"]")
search = "".join(s)
# print(search)
if type(eval(search)) == str:
print("STRING",eval(search))
elif type(eval(search)) == dict:
print("OBJECT")
except Exception:
print('NOTEXIST')
推荐阅读