201709-3 JSON查询

#include<bits/stdc++.h>
#define ll long long
#define x first
#define y second
#define ios ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
using namespace std;
int n,m;
string str;
struct node
{
    int type;//1 string 2 obj
    string t;
    node(){}
    node(string tem)
    {
        type=1;
        t=tem;
    }
    map<string,node>v;
};
/*void pro()
{
    for(int i=0;i<str.length()-1;i++)
        if(str[i]=='\\'&&str[i+1]=='"'||str[i]=='\\'&&str[i+1]=='\\')
             str.erase(i,1);
}*/
string get_str(int &k)
{
    string w="";
    k++;
    while(str[k]!='"')
    {
        if(str[k]=='\\')
            {
                w+=str[k+1];
                k+=2;
            }
        else
        {
            w+=str[k];
            k++;
        }
        //cout<<w<<endl;
    }
    k++;
    //cout<<w<<endl;
    return w;
}
node work_obj(int &k);
void work_kv(node &obj,int &k)
{
    string key=get_str(k);
    //cout<<key<<endl;
    while(str[k]!=','&&str[k]!='}')
    {
        if(str[k]=='"') obj.v[key]=node(get_str(k));
        else if(str[k]=='{')
            {
                obj.v[key]=work_obj(k);
                if(k+1<str.length()&&str[k+1]==',') k++;
            }
        else k++;
    }
    //cout<<str[k+1]<<endl;
}
node work_obj(int &k)
{
    node obj;
    obj.type=2;
    while(str[k]!='{') k++;
    while(str[k]!='}')
    {
        if(str[k]=='"')
            {
                work_kv(obj,k);
            }
        else k++;
    }
    return obj;
}
void query(node obj,vector<string> g)
{
    node o;
    string p;
    for(int j=0;j<g.size();j++)
    {
        string i=g[j];
        p=i;
        //cout<<p<<endl;
        if(obj.v.find(p)==obj.v.end())
        {
            cout<<"NOTEXIST"<<endl;
            return;
        }
        o=obj.v[p];
        obj=o;
    }
    if(obj.type==1) cout<<"STRING "<<obj.t<<endl;
    else cout<<"OBJECT"<<endl;
}
int main()
{
    cin>>n>>m;
    string tem;
    getchar();
    for(int i=0;i<n;i++)
    {
        getline(cin,tem);
        str+=tem;
    }
    //pro();
    int k=0;
    node obj=work_obj(k);
    for(int i=0;i<m;i++)
    {
        int j=0;
        string y;
        vector<string>g;
        getline(cin,y);
        for(int u=0;u<y.length();u++)
        {
            j=u;
            while(j<y.length()&&y[j]!='.') j++;
            //cout<<y.substr(u,j-u)<<endl;
            g.push_back(y.substr(u,j-u));
            u=j;
        }
        query(obj,g);
    }
    return 0;
}

python

from sys import stdin
n,m=stdin.readline().split(' ')
n=int(n)
m=int(m)
str=''
while n:
    n-=1
    str+=stdin.readline()
obj=eval(str)

def query(o,s):
    x=o
    for i in range(len(s)-1):
        t=s[i]
        if t not in x.keys():
            print("NOTEXIST")
            return
        elif type(x[t]) is dict:
            x=x[t]
        else:
            print("NOTEXIST")
            return

    t = s[-1]
    if t not in x.keys():
        print("NOTEXIST")
    elif type(x[t]) is dict:
        print("OBJECT")
    else:
        print("STRING " + x[t])

while m:
    m-=1
    s=stdin.readline()
    s=s.strip('\n')
    s=s.split('.')
    query(obj,s)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值