-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparseJSON.java
162 lines (114 loc) · 5.02 KB
/
parseJSON.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.example.cardscanner;
import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
public class parseJSON {
private static final String TAG = parseJSON.class.getSimpleName();
public static String currentJsonStr;
public static HashMap<String, Student> codeToStudent;
public static Student currentStudent;
public static HashMap<String, StudentMetroInfo> codeToStudentMetro;
public static StudentMetroInfo currentStudentMetroCard;
public static boolean metroCard = false;
public static void fetchStudentInfoFromCode(String studentCode, Context context) {
if (codeToStudent == null) //first time only, supposing we're dealing with one JSON file only
loadJSONIntoMap(context);
currentStudent = codeToStudent.get(studentCode);
}
public static void fetchStudentInfoFromCodeMetro(String studentCode, Context context) {
if (codeToStudentMetro == null) //first time only, supposing we're dealing with one JSON file only
loadJSONIntoMapMetro(context);
currentStudentMetroCard = codeToStudentMetro.get(studentCode);
}
private static void loadJSONIntoMap(Context context) {
//load the json string from the json file of names list
currentJsonStr = loadJSONFromAssets(context);
try {
//parse the json string
JSONArray jsonArray = new JSONArray(currentJsonStr);
codeToStudent = new HashMap<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject currentStudent = (JSONObject) jsonArray.get(i);
int BN, Section;
String Code, Name, Grade;
Code = currentStudent.getString("Code");
BN = currentStudent.getInt("BN");
Section = currentStudent.getInt("Section");
Name = currentStudent.getString("Name");
Grade = currentStudent.getString("Grade");
//add the current student to the map
Student student = new Student(Name, Grade, BN, Code, Section);
codeToStudent.put(Code, student);
}
} catch (JSONException e) {
Log.e(TAG, "Something wrong with parsing the JSON");
e.printStackTrace();
}
}
private static void loadJSONIntoMapMetro(Context context) {
//load the json string from the json file of names list
currentJsonStr = loadJSONFromAssetsMetro(context);
try {
//parse the json string
JSONArray jsonArray = new JSONArray(currentJsonStr);
codeToStudentMetro = new HashMap<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject currentStudent = (JSONObject) jsonArray.get(i);
int numTripsLeft;
String Code, Name, Faculty, University;
Code = currentStudent.getString("Code");
Name = currentStudent.getString("Name");
Faculty = currentStudent.getString("Faculty");
University = currentStudent.getString("University");
numTripsLeft = currentStudent.getInt("TripsLeft");
//add the current student to the map
StudentMetroInfo student = new StudentMetroInfo(Name, Code, University, Faculty, numTripsLeft);
codeToStudentMetro.put(Code, student);
}
} catch (JSONException e) {
Log.e(TAG, "Something wrong with parsing the JSON");
e.printStackTrace();
}
}
private static String loadJSONFromAssets(Context context) {
String jsonStr = null;
try {
InputStream is = context.getAssets().open("JSON/CSE19_NamesList.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
jsonStr = new String(buffer, StandardCharsets.UTF_8);
} catch (IOException ex) {
Log.e(TAG, "couldn't load the json string");
ex.printStackTrace();
return null;
}
Log.i(TAG, "parsed the JSON successfully!");
return jsonStr;
}
private static String loadJSONFromAssetsMetro(Context context) {
String jsonStr = null;
try {
InputStream is = context.getAssets().open("JSON/Metro_NamesList.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
jsonStr = new String(buffer, StandardCharsets.UTF_8);
} catch (IOException ex) {
Log.e(TAG, "couldn't load the json string");
ex.printStackTrace();
return null;
}
Log.i(TAG, "parsed the JSON successfully!");
return jsonStr;
}
}