List of usage examples for org.json JSONObject getInt
public int getInt(String key) throws JSONException
From source file:gmc.hotplate.util.JsonParser.java
public Step parseStepObject(JSONObject obj) { Step step = null;//w w w . j a v a 2 s . c o m try { int stepId = obj.getInt(TAG_STEP_ID); String stepDescription = obj.getString(TAG_STEP_DESCRIPTION); int seconds = obj.getInt(TAG_STEP_TIME); step = new Step(stepId, stepDescription, seconds); } catch (JSONException e) { Log.w(LOG_TAG, "ParseStep() error: " + e.getMessage()); } return step; }
From source file:pubsub.io.processing.Pubsub.java
/** * React to messages.../*ww w. j a v a2 s . co m*/ */ @Override public void onMessage(JSONObject msg) { if (DEBUG) System.out.println(DEBUGTAG + msg.toString()); int callback_id = 0; callback_id = msg.getInt("id"); if (msg.optJSONObject("doc") != null) { JSONObject doc = msg.getJSONObject("doc"); // Get the callback method Method eventMethod = callbacks.get(callback_id); if (eventMethod != null) { try { // Invoke only if the method existed eventMethod.invoke(myParent, doc); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } else if (msg.optJSONArray("doc") != null) { JSONArray doc = msg.getJSONArray("doc"); // Get the callback method Method eventMethod = callbacks.get(callback_id); if (eventMethod != null) { try { // Invoke only if the method existed eventMethod.invoke(myParent, doc); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } else { // Neither... } }
From source file:ti.mobileapptracker.MobileapptrackerModule.java
private List<MATEventItem> convertToMATEventItems(Object[] arrItemMaps) { List<MATEventItem> listItems = new ArrayList<MATEventItem>(); try {/*from ww w. jav a 2 s. c o m*/ JSONArray arr = new JSONArray(Arrays.toString(arrItemMaps)); for (int i = 0; i < arr.length(); i++) { JSONObject item = arr.getJSONObject(i); String itemName = item.getString("item"); int quantity = 0; double unitPrice = 0; double revenue = 0; String attribute1 = null; String attribute2 = null; String attribute3 = null; String attribute4 = null; String attribute5 = null; if (item.has("quantity")) { quantity = item.getInt("quantity"); } if (item.has("unit_price")) { unitPrice = item.getDouble("unit_price"); } if (item.has("revenue")) { revenue = item.getDouble("revenue"); } if (item.has("attribute_sub1")) { attribute1 = item.getString("attribute_sub1"); } if (item.has("attribute_sub2")) { attribute2 = item.getString("attribute_sub2"); } if (item.has("attribute_sub3")) { attribute3 = item.getString("attribute_sub3"); } if (item.has("attribute_sub4")) { attribute4 = item.getString("attribute_sub4"); } if (item.has("attribute_sub5")) { attribute5 = item.getString("attribute_sub5"); } MATEventItem eventItem = new MATEventItem(itemName, quantity, unitPrice, revenue, attribute1, attribute2, attribute3, attribute4, attribute5); listItems.add(eventItem); } } catch (JSONException e) { e.printStackTrace(); } return listItems; }
From source file:com.phonegap.App.java
/** * Load the url into the webview./*from ww w .ja va 2 s.c o m*/ * * @param url * @param props Properties that can be passed in to the DroidGap activity (i.e. loadingDialog, wait, ...) * @throws JSONException */ public void loadUrl(String url, JSONObject props) throws JSONException { System.out.println("App.loadUrl(" + url + "," + props + ")"); int wait = 0; // If there are properties, then set them on the Activity if (props != null) { JSONArray keys = props.names(); for (int i = 0; i < keys.length(); i++) { String key = keys.getString(i); if (key.equals("wait")) { wait = props.getInt(key); } else { Object value = props.get(key); if (value == null) { } else if (value.getClass().equals(String.class)) { this.ctx.getIntent().putExtra(key, (String) value); } else if (value.getClass().equals(Boolean.class)) { this.ctx.getIntent().putExtra(key, (Boolean) value); } else if (value.getClass().equals(Integer.class)) { this.ctx.getIntent().putExtra(key, (Integer) value); } } } } // If wait property, then delay loading if (wait > 0) { ((DroidGap) this.ctx).loadUrl(url, wait); } else { ((DroidGap) this.ctx).loadUrl(url); } }
From source file:com.dzt.uberclone.HomeFragment.java
private void postSendUberRequest(String json) { try {/*from ww w. j ava 2s . c o m*/ JSONObject jsonObject = new JSONObject(json); int pendingrideid = jsonObject.getInt("pending_ride_id"); waitForUberDriver(pendingrideid); } catch (JSONException e) { e.printStackTrace(); } showMSG("Waiting for a driver to accept"); requestuber.setVisibility(View.INVISIBLE); }
From source file:io.crate.frameworks.mesos.api.CrateRestResource.java
@Nullable String mesosMasterAddress() {/*from www.j a v a 2 s .c o m*/ try (CuratorFramework cf = zkClient()) { cf.start(); List<String> children = cf.getChildren().forPath("/mesos"); List<Integer> masterIds = new ArrayList<Integer>(); if (children.isEmpty()) { return null; } JSONObject cfData = null; for (String child : children) { if (child.startsWith("json.info")) { masterIds.add(Integer.parseInt(child.split("_")[1])); } } Collections.sort(masterIds); for (String child : children) { if (child.endsWith(String.valueOf(masterIds.get(0)))) { cfData = new JSONObject(new String(cf.getData().forPath("/mesos/" + child))); break; } } if (cfData != null) { JSONObject address = cfData.getJSONObject("address"); return String.format("%s:%d", address.getString("ip"), address.getInt("port")); } } catch (Exception e) { LOGGER.error("Error while obtaining a mesos address from the curator framework: ", e); } return null; }
From source file:io.crate.frameworks.mesos.api.CrateRestResource.java
int numActiveSlaves(@Nonnull String mesosAddr) { String url = String.format("http://%s/metrics/snapshot", mesosAddr); javax.ws.rs.core.Response response = RS_CLIENT.target(url).request(MediaType.APPLICATION_JSON).get(); JSONObject clusterState = new JSONObject(response.readEntity(String.class)); return clusterState.getInt("master/slaves_active"); }
From source file:com.hyphenated.pokerplayerclient.domain.PlayerStatus.java
public PlayerStatus(JSONObject json) { try {//from w ww. java 2 s. c o m status = PlayerStatusType.valueOf(json.getString("status")); if (json.has("card1")) { this.card1 = Card.getCardByIdentifier(json.getString("card1")); this.card2 = Card.getCardByIdentifier(json.getString("card2")); } if (json.has("chips")) { this.chips = json.getInt("chips"); } if (json.has("amountBetRound")) { this.amountBetRound = json.getInt("amountBetRound"); } if (json.has("amountToCall")) { this.amountToCall = json.getInt("amountToCall"); } if (json.has("smallBlind")) { this.smallBlind = json.getInt("smallBlind"); } if (json.has("bigBlind")) { this.bigBlind = json.getInt("bigBlind"); } } catch (JSONException e) { Log.e("Poker", e.getMessage()); } }
From source file:net.dv8tion.jda.core.handle.MessageUpdateHandler.java
@Override protected Long handleInternally(JSONObject content) { if (content.has("author")) { if (content.has("type")) { MessageType type = MessageType.fromId(content.getInt("type")); switch (type) { case DEFAULT: return handleDefaultMessage(content); default: WebSocketClient.LOG/*from w w w . j av a 2 s .co m*/ .debug("JDA received a message of unknown type. Type: " + type + " JSON: " + content); return null; } } else { //TODO: handle partial message update info. Example: //From webhook/rich-embed. //{"author":{"bot":true,"id":"233501884294365184","avatar":"27ae7496b3b30cddab2feaaed06d862a","username":"GitHub","discriminator":"0000"},"id":"234838126969880596","embeds":[{"color":15109472,"author":{"icon_url":"https://avatars.githubusercontent.com/u/2415829?v=3","name":"abalabahaha","proxy_icon_url":"https://images-ext-2.discordapp.net/eyJ1cmwiOiJodHRwczovL2F2YXRhcnMuZ2l0aHVidXNlcmNvbnRlbnQuY29tL3UvMjQxNTgyOT92PTMifQ.mMTBuOMUKYowcUU1H8Gzc7g4fFQ","url":"https://github.com/abalabahaha"},"description":"It was removed from the unofficial docs and other places because devs didn't want automated registration.","type":"rich","title":"[hammerandchisel/discord-api-docs] New comment on issue #148: Register API call","url":"https://github.com/hammerandchisel/discord-api-docs/issues/148#issuecomment-252524279"}],"channel_id":"168311874624946176"} return null; } } else if (content.has("call")) { handleCallMessage(content); return null; } else return handleMessageEmbed(content); }
From source file:com.mobile.system.db.abatis.AbatisService.java
/** * //from w w w .j a v a2 s. c om * @param jsonStr * JSON String * @param beanClass * Bean class * @param basePackage * Base package name which includes all Bean classes * @return Object Bean * @throws Exception */ @SuppressWarnings({ "rawtypes", "unchecked" }) public Object parse(String jsonStr, Class beanClass, String basePackage) throws Exception { Object obj = null; JSONObject jsonObj = new JSONObject(jsonStr); // Check bean object if (beanClass == null) { Log.d(TAG, "Bean class is null"); return null; } // Read Class member fields Field[] props = beanClass.getDeclaredFields(); if (props == null || props.length == 0) { Log.d(TAG, "Class" + beanClass.getName() + " has no fields"); return null; } // Create instance of this Bean class obj = beanClass.newInstance(); // Set value of each member variable of this object for (int i = 0; i < props.length; i++) { String fieldName = props[i].getName(); // Skip public and static fields if (props[i].getModifiers() == (Modifier.PUBLIC | Modifier.STATIC)) { continue; } // Date Type of Field Class type = props[i].getType(); String typeName = type.getName(); // Check for Custom type if (typeName.equals("int")) { Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); // Set value try { m.invoke(obj, jsonObj.getInt(fieldName)); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } } else if (typeName.equals("long")) { Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); // Set value try { m.invoke(obj, jsonObj.getLong(fieldName)); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } } else if (typeName.equals("java.lang.String")) { Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); // Set value try { m.invoke(obj, jsonObj.getString(fieldName)); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } } else if (typeName.equals("double")) { Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); // Set value try { m.invoke(obj, jsonObj.getDouble(fieldName)); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } } else if (typeName.equals("java.util.Date")) { // modify Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); String dateString = jsonObj.getString(fieldName); dateString = dateString.replace(" KST", ""); SimpleDateFormat genderFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.KOREA); // Set value try { Date afterDate = genderFormat.parse(dateString); m.invoke(obj, afterDate); } catch (Exception e) { Log.d(TAG, e.getMessage()); } } else if (type.getName().equals(List.class.getName()) || type.getName().equals(ArrayList.class.getName())) { // Find out the Generic String generic = props[i].getGenericType().toString(); if (generic.indexOf("<") != -1) { String genericType = generic.substring(generic.lastIndexOf("<") + 1, generic.lastIndexOf(">")); if (genericType != null) { JSONArray array = null; try { array = jsonObj.getJSONArray(fieldName); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); array = null; } if (array == null) { continue; } ArrayList arrayList = new ArrayList(); for (int j = 0; j < array.length(); j++) { arrayList.add(parse(array.getJSONObject(j).toString(), Class.forName(genericType), basePackage)); } // Set value Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); m.invoke(obj, arrayList); } } else { // No generic defined generic = null; } } else if (typeName.startsWith(basePackage)) { Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); // Set value try { JSONObject customObj = jsonObj.getJSONObject(fieldName); if (customObj != null) { m.invoke(obj, parse(customObj.toString(), type, basePackage)); } } catch (JSONException ex) { Log.d(TAG, ex.getMessage()); } } else { // Skip Log.d(TAG, "Field " + fieldName + "#" + typeName + " is skip"); } } return obj; }