-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRead_Json.java
More file actions
23 lines (20 loc) · 737 Bytes
/
Copy pathRead_Json.java
File metadata and controls
23 lines (20 loc) · 737 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Read_Json {
public static JSONArray readJsonArrayFromFile(String fileName) throws IOException, ParseException {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(fileName));
if (obj instanceof JSONObject) {
JSONObject jsonObj = (JSONObject) obj;
return (JSONArray) jsonObj.get("Cars");
} else if (obj instanceof JSONArray) {
return (JSONArray) obj;
} else {
throw new ParseException(ParseException.ERROR_UNEXPECTED_TOKEN);
}
}
}