List of usage examples for org.json JSONObject getBoolean
public boolean getBoolean(String key) throws JSONException
From source file:com.google.cast.samples.tictactoe.GameChannel.java
/** * Processes all Text messages received from the receiver device and performs the appropriate * action for the message. Recognizable messages are of the form: * * <ul>/*from w ww .j a va2s .c om*/ * <li> KEY_JOINED: a player joined the current game * <li> KEY_MOVED: a player made a move * <li> KEY_ENDGAME: the game has ended in one of the END_STATE_* states * <li> KEY_ERROR: a game error has occurred * <li> KEY_BOARD_LAYOUT_RESPONSE: the board has been laid out in some new configuration * </ul> * * <p>No other messages are recognized. */ @Override public void onMessageReceived(CastDevice castDevice, String namespace, String message) { try { Log.d(TAG, "onTextMessageReceived: " + message); JSONObject payload = new JSONObject(message); Log.d(TAG, "payload: " + payload); if (payload.has(KEY_EVENT)) { String event = payload.getString(KEY_EVENT); if (KEY_JOINED.equals(event)) { Log.d(TAG, "JOINED"); try { String player = payload.getString(KEY_PLAYER); String opponentName = payload.getString(KEY_OPPONENT); onGameJoined(player, opponentName); } catch (JSONException e) { e.printStackTrace(); } } else if (KEY_MOVED.equals(event)) { Log.d(TAG, "MOVED"); try { String player = payload.getString(KEY_PLAYER); int row = payload.getInt(KEY_ROW); int column = payload.getInt(KEY_COLUMN); boolean isGameOver = payload.getBoolean(KEY_GAME_OVER); onGameMove(player, row, column, isGameOver); } catch (JSONException e) { e.printStackTrace(); } } else if (KEY_ENDGAME.equals(event)) { Log.d(TAG, "ENDGAME"); try { String endState = payload.getString(KEY_END_STATE); int winningLocation = -1; if (END_STATE_ABANDONED.equals(endState) == false) { winningLocation = payload.getInt(KEY_WINNING_LOCATION); } onGameEnd(endState, winningLocation); } catch (JSONException e) { e.printStackTrace(); } } else if (KEY_ERROR.equals(event)) { Log.d(TAG, "ERROR"); try { String errorMessage = payload.getString(KEY_MESSAGE); onGameError(errorMessage); } catch (JSONException e) { e.printStackTrace(); } } else if (KEY_BOARD_LAYOUT_RESPONSE.equals(event)) { Log.d(TAG, "Board Layout"); int[][] boardLayout = new int[3][3]; try { JSONArray boardJSONArray = payload.getJSONArray(KEY_BOARD); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { boardLayout[i][j] = boardJSONArray.getInt(i * 3 + j); } } onGameBoardLayout(boardLayout); } catch (JSONException e) { e.printStackTrace(); } } } else { Log.w(TAG, "Unknown payload: " + payload); } } catch (JSONException e) { Log.w(TAG, "Message doesn't contain an expected key.", e); } }
From source file:org.everit.json.schema.IntegrationTest.java
@Parameters(name = "{2}") public static List<Object[]> params() { List<Object[]> rval = new ArrayList<>(); Reflections refs = new Reflections("org.everit.json.schema.draft4", new ResourcesScanner()); Set<String> paths = refs.getResources(Pattern.compile(".*\\.json")); for (String path : paths) { if (path.indexOf("/optional/") > -1 || path.indexOf("/remotes/") > -1) { continue; }//from ww w.ja v a2 s . c o m String fileName = path.substring(path.lastIndexOf('/') + 1); JSONArray arr = loadTests(IntegrationTest.class.getResourceAsStream("/" + path)); for (int i = 0; i < arr.length(); ++i) { JSONObject schemaTest = arr.getJSONObject(i); JSONArray testcaseInputs = schemaTest.getJSONArray("tests"); for (int j = 0; j < testcaseInputs.length(); ++j) { JSONObject input = testcaseInputs.getJSONObject(j); Object[] params = new Object[5]; params[0] = "[" + fileName + "]/" + schemaTest.getString("description"); params[1] = schemaTest.get("schema"); params[2] = "[" + fileName + "]/" + input.getString("description"); params[3] = input.get("data"); params[4] = input.getBoolean("valid"); rval.add(params); } } } return rval; }
From source file:com.nginious.http.serialize.JsonSerializerTestCase.java
public void testJsonSerializer() throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ"); SerializableBean bean = new SerializableBean(); bean.setBooleanValue(true);/* www . j a v a 2s . c o m*/ bean.setDoubleValue(0.451); bean.setFloatValue(1.34f); bean.setIntValue(3400100); bean.setLongValue(3400100200L); bean.setShortValue((short) 32767); bean.setStringValue("String"); bean.setDateValue(format.parse("2011-08-24T08:50:23+0200")); Date calDate = format.parse("2011-08-24T08:52:23+0200"); Calendar cal = Calendar.getInstance(); cal.setTime(calDate); bean.setCalendarValue(cal); bean.setObjectValue(TimeZone.getDefault()); InBean inBean = new InBean(); inBean.setFirst(true); inBean.setSecond(0.567d); inBean.setThird(0.342f); inBean.setFourth(100); inBean.setFifth(3400200100L); inBean.setSixth((short) 32767); inBean.setSeventh("String"); inBean.setEight(format.parse("2011-08-25T08:50:23+0200")); cal = Calendar.getInstance(); calDate = format.parse("2011-08-25T08:52:23+0200"); cal.setTime(calDate); inBean.setNinth(cal); bean.setBeanValue(inBean); List<InBean> beanList = new ArrayList<InBean>(); beanList.add(inBean); beanList.add(inBean); bean.setBeanListValue(beanList); List<String> stringList = new ArrayList<String>(); stringList.add("One"); stringList.add("Two"); stringList.add("Three"); bean.setStringListValue(stringList); ApplicationClassLoader classLoader = new ApplicationClassLoader( Thread.currentThread().getContextClassLoader()); SerializerFactoryImpl serializerFactory = new SerializerFactoryImpl(classLoader); Serializer<SerializableBean> serializer = serializerFactory.createSerializer(SerializableBean.class, "application/json"); assertEquals("application/json", serializer.getMimeType()); StringWriter strWriter = new StringWriter(); PrintWriter writer = new PrintWriter(strWriter); serializer.serialize(writer, bean); writer.flush(); JSONObject json = new JSONObject(strWriter.toString()); assertNotNull(json); assertTrue(json.has("serializableBean")); json = json.getJSONObject("serializableBean"); assertEquals(true, json.getBoolean("booleanValue")); assertEquals(0.451, json.getDouble("doubleValue")); assertEquals(1.34f, (float) json.getDouble("floatValue")); assertEquals(3400100, json.getInt("intValue")); assertEquals(3400100200L, json.getLong("longValue")); assertEquals(32767, (short) json.getInt("shortValue")); assertEquals("String", json.getString("stringValue")); assertEquals("2011-08-24T08:50:23+02:00", json.getString("dateValue")); assertEquals("2011-08-24T08:52:23+02:00", json.getString("calendarValue")); assertEquals(TimeZone.getDefault().toString(), json.getString("objectValue")); assertTrue(json.has("beanValue")); JSONObject inBeanJson = json.getJSONObject("beanValue"); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); assertTrue(json.has("stringListValue")); JSONArray stringListJson = json.getJSONArray("stringListValue"); assertEquals("One", stringListJson.get(0)); assertEquals("Two", stringListJson.get(1)); assertEquals("Three", stringListJson.get(2)); assertTrue(json.has("beanListValue")); JSONArray beanListJson = json.getJSONArray("beanListValue"); inBeanJson = beanListJson.getJSONObject(0); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); inBeanJson = beanListJson.getJSONObject(1); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); }
From source file:com.nginious.http.serialize.JsonSerializerTestCase.java
public void testNamedJsonSerializer() throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ"); NamedBean bean = new NamedBean(); bean.setBooleanValue(true);/*from w w w .j a v a 2 s. c o m*/ bean.setDoubleValue(0.451); bean.setFloatValue(1.34f); bean.setIntValue(3400100); bean.setLongValue(3400100200L); bean.setShortValue((short) 32767); bean.setStringValue("String"); bean.setDateValue(format.parse("2011-08-24T08:50:23+0200")); Date calDate = format.parse("2011-08-24T08:52:23+0200"); Calendar cal = Calendar.getInstance(); cal.setTime(calDate); bean.setCalendarValue(cal); bean.setObjectValue(TimeZone.getDefault()); InBean inBean = new InBean(); inBean.setFirst(true); inBean.setSecond(0.567d); inBean.setThird(0.342f); inBean.setFourth(100); inBean.setFifth(3400200100L); inBean.setSixth((short) 32767); inBean.setSeventh("String"); inBean.setEight(format.parse("2011-08-25T08:50:23+0200")); cal = Calendar.getInstance(); calDate = format.parse("2011-08-25T08:52:23+0200"); cal.setTime(calDate); inBean.setNinth(cal); bean.setBeanValue(inBean); List<InBean> beanList = new ArrayList<InBean>(); beanList.add(inBean); beanList.add(inBean); bean.setBeanListValue(beanList); List<String> stringList = new ArrayList<String>(); stringList.add("One"); stringList.add("Two"); stringList.add("Three"); bean.setStringListValue(stringList); ApplicationClassLoader classLoader = new ApplicationClassLoader( Thread.currentThread().getContextClassLoader()); SerializerFactoryImpl serializerFactory = new SerializerFactoryImpl(classLoader); Serializer<NamedBean> serializer = serializerFactory.createSerializer(NamedBean.class, "application/json"); assertEquals("application/json", serializer.getMimeType()); StringWriter strWriter = new StringWriter(); PrintWriter writer = new PrintWriter(strWriter); serializer.serialize(writer, bean); writer.flush(); JSONObject json = new JSONObject(strWriter.toString()); assertNotNull(json); assertTrue(json.has("testNamedBean")); json = json.getJSONObject("testNamedBean"); assertEquals(true, json.getBoolean("testBooleanValue")); assertEquals(0.451, json.getDouble("testDoubleValue")); assertEquals(1.34f, (float) json.getDouble("testFloatValue")); assertEquals(3400100, json.getInt("testIntValue")); assertEquals(3400100200L, json.getLong("testLongValue")); assertEquals(32767, (short) json.getInt("testShortValue")); assertEquals("String", json.getString("testStringValue")); assertEquals("2011-08-24T08:50:23+02:00", json.getString("testDateValue")); assertEquals("2011-08-24T08:52:23+02:00", json.getString("testCalendarValue")); assertEquals(TimeZone.getDefault().toString(), json.getString("testObjectValue")); assertTrue(json.has("testBeanValue")); JSONObject inBeanJson = json.getJSONObject("testBeanValue"); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); assertTrue(json.has("testStringListValue")); JSONArray stringListJson = json.getJSONArray("testStringListValue"); assertEquals("One", stringListJson.get(0)); assertEquals("Two", stringListJson.get(1)); assertEquals("Three", stringListJson.get(2)); assertTrue(json.has("testBeanListValue")); JSONArray beanListJson = json.getJSONArray("testBeanListValue"); inBeanJson = beanListJson.getJSONObject(0); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); inBeanJson = beanListJson.getJSONObject(1); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); }
From source file:com.nginious.http.serialize.JsonSerializerTestCase.java
public void testEmptyJsonSerializer() throws Exception { SerializableBean bean = new SerializableBean(); ApplicationClassLoader classLoader = new ApplicationClassLoader( Thread.currentThread().getContextClassLoader()); SerializerFactoryImpl serializerFactory = new SerializerFactoryImpl(classLoader); Serializer<SerializableBean> serializer = serializerFactory.createSerializer(SerializableBean.class, "application/json"); assertEquals("application/json", serializer.getMimeType()); StringWriter strWriter = new StringWriter(); PrintWriter writer = new PrintWriter(strWriter); serializer.serialize(writer, bean);//from ww w. j ava 2s.c om writer.flush(); JSONObject json = new JSONObject(strWriter.toString()); assertNotNull(json); assertTrue(json.has("serializableBean")); json = json.getJSONObject("serializableBean"); assertEquals(false, json.getBoolean("booleanValue")); assertEquals(0.0d, json.getDouble("doubleValue")); assertEquals(0.0f, (float) json.getDouble("floatValue")); assertEquals(0, json.getInt("intValue")); assertEquals(0L, json.getLong("longValue")); assertEquals(0, (short) json.getInt("shortValue")); assertFalse(json.has("stringValue")); assertFalse(json.has("dateValue")); assertFalse(json.has("calendarValue")); assertFalse(json.has("objectValue")); }
From source file:com.t2.compassionMeditation.DeviceManagerActivity.java
/** * Receives a json string containing data about all of the paired sensors * the adds a new BioSensor for each one to the mBioSensors collection * /* w w w .ja v a 2s . com*/ * @param jsonString String containing info on all paired devices */ private void populateBioSensors(String jsonString) { Log.d(TAG, this.getClass().getSimpleName() + " populateBioSensors"); // Now clear it out and Re-populate it. mBioSensors.clear(); try { JSONArray jsonArray = new JSONArray(jsonString); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); Boolean enabled = jsonObject.getBoolean("enabled"); String name = jsonObject.getString("name"); String address = jsonObject.getString("address"); int connectionStatus = jsonObject.getInt("connectionStatus"); if (name.equalsIgnoreCase("system")) { mBluetoothEnabled = enabled; } else { Log.i(TAG, "Adding sensor " + name + ", " + address + (enabled ? ", enabled" : ", disabled") + " : " + Util.connectionStatusToString(connectionStatus)); Log.i(TAG, this.getClass().getSimpleName() + " Adding sensor " + name + ", " + address + (enabled ? ", enabled" : ", disabled")); BioSensor bioSensor = new BioSensor(name, address, enabled); bioSensor.mConnectionStatus = connectionStatus; mBioSensors.add(bioSensor); } } } catch (JSONException e) { Log.e(TAG, e.toString()); } }
From source file:com.extremeboredom.wordattack.utils.JSONUtils.java
/** * get Boolean from jsonObject// w w w .ja va 2s . c om * * @param jsonObject * @param key * @param defaultValue * @return <ul> * <li>if jsonObject is null, return defaultValue</li> * <li>if key is null or empty, return defaultValue</li> * <li>return {@link JSONObject#getBoolean(String)}</li> * </ul> */ public static boolean getBoolean(JSONObject jsonObject, String key, Boolean defaultValue) { if (jsonObject == null || StringUtils.isEmpty(key)) { return defaultValue; } try { return jsonObject.getBoolean(key); } catch (JSONException e) { if (isPrintException) { e.printStackTrace(); } return defaultValue; } }
From source file:actuatorapp.ActuatorApp.java
public ActuatorApp() throws IOException, JSONException { //Takes a sting with a relay name "RELAYLO1-10FAD.relay1" //Actuator a = new Actuator("RELAYLO1-12854.relay1"); //Starts the virtualhub that is needed to connect to the actuators Process process = new ProcessBuilder("src\\actuatorapp\\VirtualHub.exe").start(); //{"yocto_addr":"10FAD","payload":{"value":true},"type":"control"} api = new Socket("10.42.72.25", 8082); OutputStreamWriter osw = new OutputStreamWriter(api.getOutputStream(), StandardCharsets.UTF_8); InputStreamReader isr = new InputStreamReader(api.getInputStream(), StandardCharsets.UTF_8); //Sends JSON authentication to CommandAPI JSONObject secret = new JSONObject(); secret.put("type", "authenticate"); secret.put("secret", "testpass"); osw.write(secret.toString() + "\r\n"); osw.flush();/*from w w w .jav a 2s .c o m*/ System.out.println("sent"); //Waits and recieves JSON authentication response BufferedReader br = new BufferedReader(isr); JSONObject response = new JSONObject(br.readLine()); System.out.println(response.toString()); if (!response.getBoolean("success")) { System.err.println("Invalid API secret"); System.exit(1); } try { while (true) { //JSON object will contain message from the server JSONObject type = getCommand(br); //Forward the command to be processed (will find out which actuators to turn on/off) commandFromApi(type); } } catch (Exception e) { System.out.println("Error listening to api"); } }
From source file:actuatorapp.ActuatorApp.java
public void commandFromApi(JSONObject command) throws JSONException { //Takes the command and processes it try {//from ww w . j a va2 s .com String type = (String) command.get("type"); if (type.equals("control") == true) { System.out.println(command); String yocto_addr = (String) command.get("yocto_addr"); JSONObject payload = command.getJSONObject("payload"); boolean value = (Boolean) payload.getBoolean("value"); setActuatorState(yocto_addr, value); } else { System.out.println("Wrong command recieved"); } } catch (Exception e) { System.out.println("Error on command SEND"); } }
From source file:re.notifica.cordova.NotificarePlugin.java
/** * Mark a inbox item as read//from w w w . j a va 2 s .c o m * @param args * @param callbackContext */ protected void markInboxItem(JSONArray args, final CallbackContext callbackContext) { Log.i(TAG, "mark inbox item"); if (Notificare.shared().getInboxManager() != null) { try { JSONObject item = args.getJSONObject(0); item.put("_id", item.getString("itemId")); item.put("opened", item.getBoolean("status")); item.put("time", item.getString("timestamp")); NotificareInboxItem inboxItem = new NotificareInboxItem(item); Notificare.shared().getEventLogger() .logOpenNotification(inboxItem.getNotification().getNotificationId()); Notificare.shared().getInboxManager().markItem(inboxItem); if (callbackContext == null) { return; } callbackContext.success(); } catch (JSONException e) { if (callbackContext == null) { return; } callbackContext.error("JSON parse error"); } } else { if (callbackContext == null) { return; } callbackContext.error("No inbox manager"); } }