Browse Source

Added JSONParser.java to handle read and search for key events.

remotes/origin/locations
David Hermann 2 years ago
parent
commit
7d3da78ded
  1. 30
      src/main/java/org/bitbiome/JsonParser.java
  2. 7
      src/main/java/org/bitbiome/Main.java

30
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;
}
}

7
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;

Loading…
Cancel
Save