1. 程式人生 > >CCF-Json查詢Java

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;
	}

}