From 7d3da78ded538a758762f0acef608fdf029ce895 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 4 Jan 2023 02:39:30 +0100 Subject: [PATCH] Added JSONParser.java to handle read and search for key events. --- src/main/java/org/bitbiome/JsonParser.java | 30 ++++++++++++++++++++++ src/main/java/org/bitbiome/Main.java | 7 ++++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/bitbiome/JsonParser.java diff --git a/src/main/java/org/bitbiome/JsonParser.java b/src/main/java/org/bitbiome/JsonParser.java new file mode 100644 index 0000000..5852e7e --- /dev/null +++ b/src/main/java/org/bitbiome/JsonParser.java @@ -0,0 +1,30 @@ +package org.bitbiome; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONTokener; + +import java.io.InputStream; + +public class JsonParser { + public static JSONObject getJSONObject(String fileName) { + String resourceName = "./../../" + fileName; + InputStream is = JsonParser.class.getResourceAsStream(resourceName); + if (is == null) { + throw new NullPointerException("Cannot find resource file " + resourceName); + } + + JSONTokener tokener = new JSONTokener(is); + JSONObject object = new JSONObject(tokener); + System.out.println("Name: " + object.getString("name")); + + System.out.println("Inventory: "); + JSONArray inventory = object.getJSONArray("inventory"); + + for (int i = 0; i < inventory.length(); i++) { + JSONObject invObj = inventory.getJSONObject(i); + System.out.println(" - " + invObj.getString("name")); + } + return object; + } +} diff --git a/src/main/java/org/bitbiome/Main.java b/src/main/java/org/bitbiome/Main.java index 99b078d..ceb58c8 100644 --- a/src/main/java/org/bitbiome/Main.java +++ b/src/main/java/org/bitbiome/Main.java @@ -1,10 +1,15 @@ package org.bitbiome; +import org.json.JSONObject; + +import java.util.Scanner; + public class Main { + private Scanner s = new Scanner(System.in); public static void main(String[] args) { - System.out.println("Hello World!"); + JSONObject playerConfig = JsonParser.getJSONObject("playerconfig.json"); } public static int getLucky() { return 7;