-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsecheckin.java
executable file
·84 lines (68 loc) · 2.03 KB
/
parsecheckin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class parsecheckin {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
readandprocess();
}
private static Object numit(Object object) {
int total=0;
String t = object.toString();
String[] a = t.split(",");
for(int i =0;i<a.length;i++)
{
int count = Integer.parseInt(a[i].split(":")[1].charAt(0)+"");
total+=count;
}
return total;
}
private static void readandprocess() throws IOException
{
File file = new File("filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write("business_id"+"\t"+"checkin_info"+"\n");
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("testing.json"));
while ((sCurrentLine = br.readLine()) != null) {
process(sCurrentLine,bw);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
bw.close();
}
private static void process(String sCurrentLine, BufferedWriter bw) throws IOException {
String s = "["+sCurrentLine+"]";
Object obj=JSONValue.parse(s);
JSONArray array=(JSONArray)obj;
//System.out.println("=========================");
//System.out.println(array.get(0));
JSONObject obj2=(JSONObject)array.get(0);
bw.write(obj2.get("business_id")+"\t"+numit(obj2.get("checkin_info"))+"\n");
//System.out.println(numit(obj2.get("checkin_info")));
//System.out.println("=========================");
}
}