public static void main(String[] args) { JsonParser parser = new JsonParser(); try { JsonObject object = (JsonObject) parser.parse(new FileReader("test.json")); System.out.println("cat=" + object.get("cat").getAsString()); System.out.println("pop=" + object.get("cat").getAsBoolean()); JsonArray array = object.get("languages").getAsJsonArray(); for (int i = 0; i < array.size(); i++) { System.out.println("----------------------"); JsonObject subObject = array.get(i).getAsJsonObject(); System.out.println("id=" + subObject.get("id").getAsInt()); System.out.println("name=" + subObject.get("name").getAsString()); System.out.println("ide=" + subObject.get("ide").getAsString()); } } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
/** * lets an instance of Spawner spawn testlevel.json and checks whether the specified Moveables * have been created and are in the right position. */ public void testSpawning() { GS.enemys = new Vector<Moveable>(); GS.friendlys = new Vector<Moveable>(); ; initGL(); GS.deltaUpdater = new DeltaUpdater(); Spawner s = new Spawner("testlevel", GS.deltaUpdater); s.update(400); Vector<Observer> observers = null; try { Field observerField = DeltaUpdater.class.getDeclaredField("observers"); observerField.setAccessible(true); observers = (Vector<Observer>) observerField.get(GS.deltaUpdater); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } Gson g = new Gson(); HashMap<BigDecimal, Spawn[]> LevelMap = null; try { LevelMap = g.fromJson( new FileReader("json/level/testlevel.json"), (new TypeToken<HashMap<BigDecimal, Spawn[]>>() {}).getType()); } catch (JsonIOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonSyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Spawn[] SpawnArray = LevelMap.get(LevelMap.keySet().toArray()[0]); assertTrue(observers.size() == SpawnArray.length + 1); // +1 weil Spawner selbst registriert ist for (int i = 0; i < observers.size() - 1; ++i) { assertTrue( ((Entity) observers.get(i + 1)).getClass().getName().equals(SpawnArray[i].Moveable)); assertTrue(((Entity) observers.get(i + 1)).posX == SpawnArray[i].x); assertTrue(((Entity) observers.get(i + 1)).posY == SpawnArray[i].y); // assertTrue(((Entity)observers.get(i)).posX == al.get(i).x); } }
protected List<CardData> loadRawData() { try { return gson.fromJson(readLibrarySource(), getType()); } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } return Collections.emptyList(); }
@Override public Deck loadDeck(String name) { DeckData data; try { data = gson.fromJson(readSource(getDeckUri(name)), DeckData.class); CardLibrary cl = loadCardLibrary(); Card identityCard = cl.getCard(data.getIdentity()); Deck deck = new Deck(cl.getItentity(identityCard.getKey()), data.getName()); for (CardRef ref : data.getCards()) { deck.add(cl.getCard(ref.getCard()), ref.getCount()); } return deck; } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; }
public static void init() { gson = new Gson(); try { BufferedReader r = new BufferedReader(new FileReader("conf/level.json")); // r.readLine(); levelData = gson.fromJson(r, Levels.class); r.close(); } catch (JsonSyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonIOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override public void ImportWarningCategories() { List<WarningCategory> warningCategories = new ArrayList<WarningCategory>(); try { Reader reader = new InputStreamReader( AdminServiceImpl.class .getClassLoader() .getResourceAsStream("mongo/warningCategories.json")); warningCategories = new Gson().fromJson(reader, new TypeToken<List<WarningCategory>>() {}.getType()); } catch (JsonSyntaxException e) { log.error(e.getMessage(), e); throw new MedCheckerException(e.getMessage()); } catch (JsonIOException e) { log.error(e.getMessage(), e); throw new MedCheckerException(e.getMessage()); } warningRepo.insert(warningCategories); }
public void sendLoginJsonRequest() { if (!Connectivity.isConnected(getApplicationContext())) { Snackbar.make( loginLayout, "Something is wrong with your internet connection. Please check your settings", Snackbar.LENGTH_LONG) .show(); } else { progressWheel.setVisibility(View.VISIBLE); final JsonObject loginObject = new JsonObject(); final JsonObject jsonObject1 = new JsonObject(); final JsonObject deviceObject = new JsonObject(); try { jsonObject1.addProperty("email", email.getText().toString()); jsonObject1.addProperty("password", password.getText().toString()); loginObject.add("payload", jsonObject1); loginObject.addProperty("token", preferenceClass.getAccessToken()); deviceObject.addProperty("device_name", deviceInfo.getDeviceName()); deviceObject.addProperty("device_type", "ANDROID"); deviceObject.addProperty("device_os_ver", deviceInfo.getDeviceOSVersion()); deviceObject.addProperty( "device_resolution", getResources().getDisplayMetrics().widthPixels + "*" + getResources().getDisplayMetrics().heightPixels); deviceObject.addProperty("device_token", preferenceClass.getDeviceToken()); deviceObject.addProperty("app_ver", BuildConfig.VERSION_NAME); deviceObject.addProperty("lon", ""); deviceObject.addProperty("lat", ""); deviceObject.addProperty("user_id", ""); jsonObject1.add("device", deviceObject); } catch (JsonIOException e) { e.printStackTrace(); } Ion.with(getApplicationContext()) .load(getLoginRequestUrl()) .setJsonObjectBody(loginObject) .asJsonObject() .withResponse() .setCallback( new FutureCallback<com.koushikdutta.ion.Response<JsonObject>>() { @Override public void onCompleted( Exception e, com.koushikdutta.ion.Response<JsonObject> result) { progressWheel.setVisibility(View.GONE); if (e != null) { Log.e("exception", e.getLocalizedMessage() + " " + e.getLocalizedMessage()); hideKeyboard(); Snackbar.make( loginLayout, "We are facing problems in connecting to our servers. Just chill and try after some time :)", Snackbar.LENGTH_LONG) .show(); } else if (result != null) { xxx = result.getHeaders().getHeaders().getAll("Set-Cookie"); // xxx = result.getHeaders().getHeaders().getAll("Set-Cookie"); if (xxx.size() != 0) { size = xxx.size(); splitCookie = xxx.get(size - 1).split(";"); splitSessionId = splitCookie[0].split("="); preferenceClass.saveCookie("laravel_session=" + splitSessionId[1]); } parseJsonData(result.getResult()); } else { Log.e("Exception", "There is a problem"); hideKeyboard(); Snackbar.make( loginLayout, "Something is wrong with the internet connection", Snackbar.LENGTH_LONG) .show(); } } }); } }