Update JsonToJava.java

This commit is contained in:
Boyeau Indy 2021-06-03 08:22:32 +00:00
parent eb024153a8
commit 9200ae0415

View File

@ -8,39 +8,60 @@ public class JsonToJava {
public static void main(String args[]) { public static void main(String args[]) {
JsonRecover();
}
private static void JsonRecover() {
// initialize the parser // initialize the parser
JSONParser jsonP = new JSONParser(); JSONParser jsonP = new JSONParser();
try { try {
// read the json document // read the json document
JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("src/configuration/config.json")); JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("src/configuration/config.json"));
//to print all values // to print all values
//System.out.println(jsonO.values()); // System.out.println(jsonO.values());
// isolate the "test" part and print it // isolate the "test" part and print it
// String test = (String) jsonO.get("test"); // String test = (String) jsonO.get("test");
// System.out.println("ceci est un test :" + test); // System.out.println("ceci est un test :" + test);
// arena selection
// select an element on the list
JSONArray arena = (JSONArray) jsonO.get("arena");
// print a case of this element
System.out.println("arena : " + arena.get(1));
//select an element on the list // nb players selection
JSONArray arene = (JSONArray) jsonO.get("arena");
//print a case of this element
System.out.println(arene.get(1));
JSONArray nb_players = (JSONArray) jsonO.get("nb_players"); JSONArray nb_players = (JSONArray) jsonO.get("nb_players");
System.out.println(nb_players.get(1)); System.out.println("nb_player : " + nb_players.get(1));
// character selection
JSONArray character1 = (JSONArray) jsonO.get("character1"); JSONArray character1 = (JSONArray) jsonO.get("character1");
System.out.println(character1.get(1)); System.out.println("players 1 : " + character1.get(1));
JSONArray character2 = (JSONArray) jsonO.get("character2"); JSONArray character2 = (JSONArray) jsonO.get("character2");
System.out.println(character2.get(1)); System.out.println("players 2 : " + character2.get(1));
// resolution
JSONArray resolution = (JSONArray) jsonO.get("resolution"); JSONArray resolution = (JSONArray) jsonO.get("resolution");
System.out.println(resolution.get(1)); // resolution string " width x heigth"
JSONObject reso = (JSONObject) resolution.get(1);
JSONArray button = (JSONArray) jsonO.get("button"); String heightStr = (String) reso.get("height");
System.out.println(button); int height = Integer.parseInt(heightStr); // String to int
String widthStr = (String) reso.get("width");
int width = Integer.parseInt(widthStr);
System.out.println("heigth : " + height + " width : " + width);
// button selection
JSONArray allButton = (JSONArray) jsonO.get("button");
System.out.println(allButton);
String up = (String) allButton.get(0);
System.out.println("button for up is : " + up);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();