欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CCF CSP 201709-3 JSON查询 python

程序员文章站 2024-03-17 21:34:40
...

CCF CSP 201709-3 JSON查询 pythonCCF CSP 201709-3 JSON查询 python

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')

相关标签: CCF CSP python