This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonReader.java
156 lines (132 loc) · 4.8 KB
/
JsonReader.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
// =============================================================================
// Filename: JsonReader.java ~ November 24, 2019
// =============================================================================
// Jaime Bohorquez
// Created using Atom + iTerm2 on Mac OS
// This program stores the Team class, that is able to instantiate a team object
// that can store the teams score, name, etc.
// =============================================================================
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
class JsonReader
{
protected static Team getRobotEventsScores(String fileName)
{
JSONParser parser = new JSONParser();
Reader reader;
JSONArray array = null;
try
{
// Lets get the array within the json file.
reader = new FileReader(fileName);
array = (JSONArray) parser.parse(reader);
}
catch (Exception e)
{
e.printStackTrace();
}
// lets get the team array
Team head = null;
Team current = null;
for (int i = 0; i < array.size(); i++)
{
// Gets the team from the array.
JSONObject jsonTeam = (JSONObject) array.get(i);
JSONObject teamData = (JSONObject) jsonTeam.get("team");
String teamName = (String) teamData.get("teamName");
String plateNumber = (String) teamData.get("team");
JSONObject scores = (JSONObject) jsonTeam.get("scores");
long score = (long) scores.get("score");
Team team = new Team(teamName, plateNumber, (int) score);
if (head == null)
{
current = team;
head = current;
}
else
{
current.next = team;
current = current.next;
}
}
return head;
}
protected static Team getVexDBScores(String fileName)
{
JSONParser parser = new JSONParser();
Reader reader;
JSONArray array = null;
int size = 0;
try
{
// Lets get the array within the json file.
reader = new FileReader(fileName);
JSONObject container = (JSONObject) parser.parse(reader);
long length = (long) container.get("size");
size = (int) length;
array = (JSONArray) container.get("result");
}
catch (Exception e)
{
e.printStackTrace();
}
// lets get the team array
Team head = null, current = null;
for (int i = 0; i < size; i++)
{
// Gets the team from the array.
JSONObject jsonTeam = (JSONObject) array.get(i);
String plateNumber = (String) jsonTeam.get("team");
String teamName = getTeamName(plateNumber);
long score = (long) jsonTeam.get("score");
Team team = new Team(teamName, plateNumber, (int) score);
if (head == null)
{
current = team;
head = current;
}
else
{
current.next = team;
current = current.next;
}
}
return head;
}
protected static String getTeamName(String plateNumber)
{
String fileName = "./data/team_data.json";
String link = "https://api.vexdb.io/v1/get_teams?team=" + plateNumber;
URLWritter.writeToFile(link, fileName);
// Read JSON file.
JSONParser parser = new JSONParser();
Reader reader;
JSONArray array = null;
int size = 0;
try
{
// Lets get the array within the json file.
reader = new FileReader(fileName);
JSONObject container = (JSONObject) parser.parse(reader);
long length = (long) container.get("size");
size = (int) length;
array = (JSONArray) container.get("result");
}
catch (Exception e)
{
e.printStackTrace();
}
// lets get the team array
Team head = null, current = null;
// Gets the team from the array.
JSONObject jsonTeam = (JSONObject) array.get(0);
String name = (String) jsonTeam.get("team_name");
return name;
}
}