Implement JSON file parser

This commit is contained in:
coolneng 2020-12-15 12:12:30 +01:00
parent 300b7b78ee
commit 80d7675059
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 15 additions and 8 deletions

View File

@ -2,12 +2,19 @@ package org.RI.P2;
import java.util.List;
import java.io.IOException;
import java.io.Reader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import org.json.simple.JSONArray;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
public class Parser {
private static List<File> files;
@ -16,17 +23,17 @@ public class Parser {
.collect(Collectors.toList());
}
public JSONArray parseJSONFile(String filePath) throws IOException, ParseException {
InputStream jsonFile = getClass().getResourceAsStream(filePath);
Reader readerJson = new InputStreamReader(jsonFile);
Object fileObjects = JSONValue.parseWithException(readerJson);
JSONArray arrayObjects = (JSONArray) fileObjects;
return arrayObjects;
}
private static void usage() {
System.out.println("Usage: Parser <directory>");
System.out.println("option directory: directory that contains JSON files");
System.exit(1);
}
public static void main(String[] args) throws IOException {
if (args.length != 1) {
usage();
}
String directory = args[0];
readFiles(directory);
}
}