We can parse the JSON now!

This commit is contained in:
Chloe Fontenot 🏳️‍⚧️ 2022-11-30 07:25:18 -06:00
parent 82d8e6f8ca
commit 3ac6eb01cf
17 changed files with 15 additions and 13 deletions

Binary file not shown.

Binary file not shown.

View File

@ -9,11 +9,8 @@ import java.util.ArrayList;
import java.util.List;
public class Common {
public static List<String> parse(String fetchURL, String keyToFetch) throws Exception {
//System.out.println("CURLing API...");
URL url = new URL(fetchURL);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
for (String line; (line = reader.readLine()) != null; ) {
public static List<String> parseKey(String fetchURL, String keyToFetch) throws Exception {
String line = rawFetch(fetchURL);
JSONObject json = new JSONObject(line);
//System.out.println(line);
JSONArray key = json.getJSONArray(keyToFetch);
@ -25,12 +22,6 @@ public class Common {
return returnList;
}
} catch (Exception IllegalArgumentException) {
return null;
}
return null;
}
public static String rawFetch(String fetchURL) throws Exception {
//System.out.println("CURLing API...");
URL url = new URL(fetchURL);
@ -48,4 +39,15 @@ public class Common {
}
return null;
}
public static String regex(String fetchURL) throws Exception{ // Regexes off the start and end square braces returned by certain APIs?
String raw = rawFetch(fetchURL);
raw = raw.substring(1, raw.length() - 1);
return raw;
}
public static String parse(String fetchURL, String keyToFetch) throws Exception{
String raw = regex(fetchURL);
JSONTokener tokener = new JSONTokener(raw);
JSONObject object = new JSONObject(tokener);
return object.getString(keyToFetch);
}
}

View File

@ -52,7 +52,7 @@ public class Main {
userInput = input.nextInt();
if (userInput == 1) { // user has selected PaperMC
System.out.println("Available Projects: ");
List<String> projects = Common.parse(PAPER_API_URL + "/projects", "projects");
List<String> projects = Common.parseKey(PAPER_API_URL + "/projects", "projects");
int i = 1;
for (String projectNames : projects) {
System.out.println(i + ". " + projectNames);

View File

@ -5,6 +5,6 @@ import com.calebfontenot.Common;
public class Velocity {
public static final String MODRINTH_API_URL = "https://api.modrinth.com/v2";
public static void fetchVersions(String modID) throws Exception { // Fetch information about mod versions from the API.
System.out.println(Common.rawFetch(MODRINTH_API_URL + "/project/" + modID + "/version"));
System.out.println(Common.parse(MODRINTH_API_URL + "/project/" + modID + "/version", "id"));
}
}