您的位置:首页 > 编程语言 > Java开发

CCF 201709-3 JSON查询(Java)

2018-02-21 13:36 447 查看
问题描述
  JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,可以用来描述半结构化的数据。
JSON 格式中的基本单元是值 (value),出于简化的目的本题只涉及 2 种类型的值:
  * 字符串 (string):字符串是由双引号 " 括起来的一组字符(可以为空)。如果字符串的内容中出
现双引号 ",在双引号前面加反斜杠,也就是用 \" 表示;如果出现反斜杠 \,则用两个反斜杠 \ 表示。
反斜杠后面不能出现 " 和 \ 以外的字符。例如:""、"hello"、"\"\"。
  * 对象 (object):对象是一组键值对的无序集合(可以为空)。键值对表示对象的属性,键是属性名,
值是属性的内容。对象以左花括号 { 开始,右花括号 } 结束,键值对之间以逗号 , 分隔。一个键值对的
键和值之间以冒号 : 分隔。键必须是字符串,同一个对象所有键值对的键必须两两都不相同;值可以是字
符串,也可以是另一个对象。例如:{}、{"foo": "bar"}、{"Mon": "weekday", "Tue": "wee
kday", "Sun": "weekend"}。
  除了字符串内部的位置,其他位置都可以插入一个或多个空格使得 JSON 的呈现更加美观,也可以在一
些地方换行,不会影响所表示的数据内容。例如,上面举例的最后一个 JSON 数据也可以写成如下形式。
  {
  "Mon": "weekday",
  "Tue": "weekday",
  "Sun": "weekend"
  }
  给出一个 JSON 格式描述的数据,以及若干查询,编程返回这些查询的结果。

输入格式
  第一行是两个正整数 n 和 m,分别表示 JSON 数据的行数和查询的个数。
  接下来 n 行,描述一个 JSON 数据,保证输入是一个合法的 JSON 对象。
  接下来 m 行,每行描述一个查询。给出要查询的属性名,要求返回对应属性的内容。需要支持多层查
询,各层的属性名之间用小数点 . 连接。保证查询的格式都是合法的。

输出格式
  对于输入的每一个查询,按顺序输出查询结果,每个结果占一行。
  如果查询结果是一个字符串,则输出 STRING ,其中 是字符串的值,中间用一个空格分隔。
  如果查询结果是一个对象,则输出 OBJECT,不需要输出对象的内容。
  如果查询结果不存在,则输出 NOTEXIST。

样例输入
10 5 

"firstName": "John", 
"lastName": "Smith", 
"address": { 
"streetAddress": "2ndStreet", 
"city": "NewYork", 
"state": "NY"
}, 
"esc\aped": "\"hello\"" 
}
firstName
address
address.city
address.postal
esc\aped
样例输出 
STRING John 
OBJECT 
STRING NewYork 
NOTEXIST 
STRING "hello"

评测用例规模与约定
  n ≤ 100,每行不超过 80 个字符。
  m ≤ 100,每个查询的长度不超过 80 个字符。
  字符串中的字符均为 ASCII 码 33-126 的可打印字符,不会出现空格。所有字符串都不是空串。
  所有作为键的字符串不会包含小数点 .。查询时键的大小写敏感。
  50%的评测用例输入的对象只有 1 层结构,80%的评测用例输入的对象结构层数不超过 2 层。举例来说,
{"a": "b"} 是一层结构的对象,{"a": {"b": "c"}} 是二层结构的对象,以此类推。
代码只支持1层结构public class C {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String line=null;
String result=null;
StringBuilder temp=new StringBuilder();
int n=sc.nextInt();
int s=sc.nextInt();
int cout=0;

while(n--!=-1){ //把原数据转换成更简单格式的数据
line=sc.nextLine();
for (int i = 0; i < line.length(); i++) { //去掉多余的空格、引号和最外面的大括号
if(line.charAt(i)=='\\'){
if(line.charAt(i+1)=='\\'||line.charAt(i+1)=='"'){
i++;
}
}else if ((line.charAt(i)==' '||line.charAt(i)=='"')) {
continue;
}else if(cout==0&&line.charAt(i)=='{'){
cout++;
continue;
}else if(cout!=0&&line.charAt(i)=='{'){
cout++;
}else if(cout==1&&line.charAt(i)=='}'){
continue;
}else if(cout>0&&line.charAt(i)=='}'){
cout--;
}
temp.append(line.charAt(i));
}

}
// System.out.println(temp.toString());

String change=temp.toString(); //已经简化的JSON格式数据
while(s--!=0){
result=sc.nextLine();
boolean flag=false; //开始记录标志
int
97e5
index=change.indexOf(result);
if(index==0){ //要查找的键是第一个
System.out.print("STRING ");
while(change.charAt(++index)!=','){
if (!flag&&change.charAt(index)==':'){ //越过冒号,记录冒号之后的数据
flag=true;
continue;
}
if(flag){
System.out.print(change.charAt(index));
}
}
System.out.println();
}else if(result.contains(".")){ //要查找的键是OBJECT
boolean l=true;
StringBuilder temp1=new StringBuilder(); //字符串分离
StringBuilder temp2=new StringBuilder();
int index1=0;
String temp3=null;
String temp4=null;
String a=null;
String b=null;
for (int i = 0; i < result.length(); i++) { //字符串分离
if(l&&result.charAt(i)!='.'){
temp1.append(result.charAt(i));
}else if(result.charAt(i)=='.'){
l=false;
}else {
temp2.append(result.charAt(i));
}
}
a=temp1.toString();
b=temp2.toString();

index=change.indexOf(a); //锁定字符串头

temp3=change.substring(index); //截断

index1=temp3.indexOf("}"); //锁定字符串尾

temp4=change.substring(index,index+index1+1); //截断

int index2=temp4.indexOf(b); //内部索引号
if(index2==-1){
System.out.println("NOTEXIST");
continue;
}else {
System.out.print("STRING ");
while (temp4.charAt(++index2)!=','&&index2!=temp4.length()-1){
if(temp4.charAt(index2)==':'&&!flag){
flag=true;
continue;
}
if (flag){
System.out.print(temp4.charAt(index2));
}
}
}
System.out.println();

}else if(index==-1){ //没有找到
System.out.println("NOTEXIST");
continue;
}else { //要查找的键是STRING
while(change.charAt(++index)!=','&&index!=change.length()-1){
if(change.charAt(index-1)==':'&&!flag){
if (change.charAt(index)=='{'){ //为OBJECT类型
System.out.print("OBJECT");
break;
}else{
System.out.print("STRING ");
flag=true;
}
}
if(flag){
System.out.print(change.charAt(index));
}
}
if(index==change.length()-1){
System.out.print(change.charAt(index));
}
System.out.println();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: