CCF-Json查询Java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class JsonQuery {
	static Scanner sc;
	static int n; // Json数据的行数
	static int m; // 待查询单的个数
	static String temp = "";
	static String[] cmd;
	static List<Rule> list;
	static List<String> outlist;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		list = new ArrayList<>();
		outlist = new ArrayList<>();
		sc.nextLine();
		for (int i = 0; i < n; i++) {
			temp += sc.nextLine().replaceAll(" ", "");
		}
		temp = temp.substring(1, temp.length() - 1);
		if (temp.contains("{")) {
			int first = temp.indexOf("{");
			int last = temp.indexOf("}");
			String mid = temp.substring(first + 1, last);
			String left = temp.substring(0, first);
			String right = temp.substring(last + 2) + ",";
			cmd = left.split(",");
			for (int i = 0; i < cmd.length - 1; i++) {
				String[] keyvalue = cmd[i].split(":");
				String key = keyvalue[0].substring(1, keyvalue[0].length() - 1);
				String value = keyvalue[1].substring(1, keyvalue[1].length() - 1);
				key = delete(key);
				value = delete(value);
				list.add(new Rule("STRING", key, value));
			}
			String key = cmd[cmd.length - 1].substring(1, cmd[cmd.length - 1].length() - 2);
			key = delete(key);
			list.add(new Rule("OBJECT", key, mid));
			cmd = right.split(",");
			sper(cmd);
		} else {
			cmd = temp.split(",");
			sper(cmd);
		}
		for (int i = 0; i < m; i++) {
			temp = sc.nextLine();
			deal();
		}
		for (int i = 0; i < outlist.size(); i++) {
			System.out.println(outlist.get(i));
		}
	}

	public static void sper(String[] cmd) {
		for (int i = 0; i < cmd.length; i++) {
			String[] keyvalue = cmd[i].split(":");
			String key = keyvalue[0].substring(1, keyvalue[0].length() - 1);
			String value = keyvalue[1].substring(1, keyvalue[1].length() - 1);
			key = delete(key);
			value = delete(value);
			list.add(new Rule("STRING", key, value));
		}
	}

	public static String delete(String str) {

		// 去除斜杠
		if (str.contains("\\")) {
			// 找到\所在的位置
			int index = str.indexOf("\\");
			// 如果反斜杠后面还有一个\
			if (str.substring(index + 1, index + 2).contains("\\")) {

				str = str.substring(0, index) + str.substring(index + 1);

			} else if (str.substring(index + 1, index + 2).contains("\"")) {
				// 如果反斜杠后面跟的是引号 找出引号所在的位置
				int index1 = str.indexOf("\\");
				int index2 = str.lastIndexOf("\\");
				str = str.substring(index1 + 1, index2) + str.substring(index2 + 1);
			}
		}
		return str;
	}
	public static void deal() {
		boolean flag = false;
		if (temp.contains(".")) {
			String[] div = temp.split("[.]");
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).key.equals(div[0])) {
					String[] templine = list.get(i).value.split(",");
					for (int j = 0; j < templine.length; j++) {
						String[] line = templine[j].split(":");
						String key = delete(line[0].substring(1, line[0].length() - 1));
						String value = delete(line[1].substring(1, line[1].length() - 1));
						if (div[1].equals(key)) {
							flag = true;
							outlist.add("STRING" + " " + value);
						}
					}
				}
			}
		} else {
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).key.equals(temp)) {
					flag = true;
					if (list.get(i).type.equals("OBJECT")) {
						outlist.add(list.get(i).type);
					} else
						outlist.add(list.get(i).type + " " + list.get(i).value);
				}
			}
		}
		if (flag == false)
			outlist.add("NOTEXIST");
	}
}

class Rule {
	public String type;
	public String key;
	public String value;

	public Rule(String type, String key, String value) {
		this.type = type;
		this.key = key;
		this.value = value;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值