List of usage examples for org.json JSONObject isNull
public boolean isNull(String key)
From source file:org.mixare.data.Json.java
public Marker processTwitterJSONObject(JSONObject jo, DataSource datasource) throws NumberFormatException, JSONException { Marker ma = null;/* www .j ava 2 s . co m*/ if (jo.has("geo")) { Double lat = null, lon = null; if (!jo.isNull("geo")) { JSONObject geo = jo.getJSONObject("geo"); JSONArray coordinates = geo.getJSONArray("coordinates"); lat = Double.parseDouble(coordinates.getString(0)); lon = Double.parseDouble(coordinates.getString(1)); } else if (jo.has("location")) { // Regex pattern to match location information // from the location setting, like: // iPhone: 12.34,56.78 // T: 12.34,56.78 // 12.34,56.78 Pattern pattern = Pattern.compile("\\D*([0-9.]+),\\s?([0-9.]+)"); Matcher matcher = pattern.matcher(jo.getString("location")); if (matcher.find()) { lat = Double.parseDouble(matcher.group(1)); lon = Double.parseDouble(matcher.group(2)); } } if (lat != null) { Log.v(MixView.TAG, "processing Twitter JSON object"); String user = jo.getString("from_user"); String url = "http://twitter.com/" + user; ma = new SocialMarker(user + ": " + jo.getString("text"), lat, lon, 0, url, datasource); } } return ma; }
From source file:org.uiautomation.ios.context.BaseWebInspector.java
private <T> T cast_(JSONObject body) throws JSONException { List<String> primitives = new ArrayList<String>(); primitives.add("boolean"); primitives.add("number"); primitives.add("string"); String type = body.getString("type"); // handle null return if ("undefined".equals(type)) { return (T) null; }//from w ww .j a v a 2s.c om // handle primitive types. if (primitives.contains(type)) { // primitive type. Object value = body.get("value"); return (T) value; } // handle objects if ("object".equals(type)) { if (body.has("value") && body.isNull("value")) { return (T) null; } if ("array".equals(body.optString("subtype"))) { RemoteObject array = new RemoteObject(body.getString("objectId"), this); RemoteObjectArray a = new RemoteObjectArray(array); ArrayList<Object> res = new ArrayList<Object>(); for (Object ro : a) { res.add(ro); } return (T) res; } if (body.has("objectId")) { if ("node".equals(body.optString("subtype")) || "Window".equals(body.optString("className"))) { return (T) new RemoteObject(body.getString("objectId"), this); } else { RemoteObject ro = new RemoteObject(body.getString("objectId"), this); JSONObject o = new JSONObject(ro.stringify()); return (T) o; } } return (T) new RemoteObject(body.getString("objectId"), this); } throw new RuntimeException("NI " + body); }
From source file:com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushIntentService.java
protected void dismissNotification(String nid) { SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(MFPPush.PREFS_NAME, Context.MODE_PRIVATE); int countOfStoredMessages = sharedPreferences.getInt(MFPPush.PREFS_NOTIFICATION_COUNT, 0); if (countOfStoredMessages > 0) { for (int index = 1; index <= countOfStoredMessages; index++) { String key = MFPPush.PREFS_NOTIFICATION_MSG + index; try { String msg = sharedPreferences.getString(key, null); if (msg != null) { JSONObject messageObject = new JSONObject(msg); if (messageObject != null && !messageObject.isNull(NID)) { String id = messageObject.getString(NID); if (id != null && id.equals(nid)) { MFPPushUtils.removeContentFromSharedPreferences(sharedPreferences, key); MFPPushUtils.storeContentInSharedPreferences(sharedPreferences, MFPPush.PREFS_NOTIFICATION_COUNT, countOfStoredMessages - 1); NotificationManager mNotificationManager = (NotificationManager) this .getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.cancel(messageObject.getInt(NOTIFICATIONID)); }/* w w w.j a va 2 s .c o m*/ } } } catch (JSONException e) { logger.error("MFPPushIntentService: dismissNotification() - Failed to dismiss notification."); } } } }
From source file:com.wordsbaking.cordova.wechat.WeChat.java
private void share(JSONArray args, CallbackContext callbackContext) throws JSONException, NullPointerException { // check if installed if (!api.isWXAppInstalled()) { callbackContext.error(ERR_WECHAT_NOT_INSTALLED); return;/*from w w w . j a v a 2 s . c o m*/ } JSONObject params = args.getJSONObject(0); if (params == null) { callbackContext.error(ERR_INVALID_OPTIONS); return; } SendMessageToWX.Req request = new SendMessageToWX.Req(); request.transaction = String.valueOf(System.currentTimeMillis()); int paramScene = params.getInt("scene"); switch (paramScene) { case SCENE_SESSION: request.scene = SendMessageToWX.Req.WXSceneSession; break; // wechat android sdk does not support chosen by user case SCENE_CHOSEN_BY_USER: case SCENE_TIMELINE: default: request.scene = SendMessageToWX.Req.WXSceneTimeline; break; } WXMediaMessage message = null; String text = null; JSONObject messageOptions = null; if (!params.isNull("text")) { text = params.getString("text"); } if (!params.isNull("message")) { messageOptions = params.getJSONObject("message"); } if (messageOptions != null) { String url = null; String data = null; if (!messageOptions.isNull("url")) { url = messageOptions.getString("url"); } if (!messageOptions.isNull("data")) { data = messageOptions.getString("data"); } int type = SHARE_TYPE_WEBPAGE; if (!messageOptions.isNull("type")) { type = messageOptions.getInt("type"); } switch (type) { case SHARE_TYPE_APP: break; case SHARE_TYPE_EMOTION: break; case SHARE_TYPE_FILE: break; case SHARE_TYPE_IMAGE: WXImageObject imageObject = new WXImageObject(); if (url != null) { imageObject.imageUrl = url; } else if (data != null) { imageObject.imageData = Base64.decode(data, Base64.DEFAULT); } else { callbackContext.error(ERR_INVALID_OPTIONS); return; } message = new WXMediaMessage(imageObject); break; case SHARE_TYPE_MUSIC: break; case SHARE_TYPE_VIDEO: break; case SHARE_TYPE_WEBPAGE: default: WXWebpageObject webpageObject = new WXWebpageObject(); webpageObject.webpageUrl = url; message = new WXMediaMessage(webpageObject); break; } if (message == null) { callbackContext.error(ERR_UNSUPPORTED_MEDIA_TYPE); return; } if (!messageOptions.isNull("title")) { message.title = messageOptions.getString("title"); } if (!messageOptions.isNull("description")) { message.description = messageOptions.getString("description"); } if (!messageOptions.isNull("thumbData")) { String thumbData = messageOptions.getString("thumbData"); message.thumbData = Base64.decode(thumbData, Base64.DEFAULT); } } else if (text != null) { WXTextObject textObject = new WXTextObject(); textObject.text = text; message = new WXMediaMessage(textObject); message.description = text; } else { callbackContext.error(ERR_INVALID_OPTIONS); return; } request.message = message; try { boolean success = api.sendReq(request); if (!success) { callbackContext.error(ERR_UNKNOWN); return; } } catch (Exception e) { callbackContext.error(e.getMessage()); return; } currentCallbackContext = callbackContext; }
From source file:net.dv8tion.jda.core.requests.ErrorResponse.java
public static ErrorResponse fromJSON(JSONObject obj) { if (obj == null || obj.isNull("code")) return SERVER_ERROR; else {// w ww . ja v a 2 s.c o m return ErrorResponse.fromCode(obj.getInt("code")); } }
From source file:com.hotstar.player.adplayer.feeds.ReferencePlayerFeedItemAdapter.java
/** * Gets field value from JSON object given field name * //ww w. j a v a 2s . com * @param jsonObject * - JSON object to search in * @param fieldName * - field name to be searched for * @return String - field value or null if field name does not exist or * field value is empty */ protected String getString(JSONObject jsonObject, String fieldName) { /* try { if (jsonObject.has(fieldName) && !jsonObject.isNull(fieldName)) { return jsonObject.getString(fieldName); } } catch (JSONException e) { AdVideoApplication.logger.e(LOG_TAG + "::getString", "Error getting field from JSON: " + e.getMessage()); } */ try { Iterator<String> iter = jsonObject.keys(); while (iter.hasNext()) { String key1 = iter.next(); if (key1.equalsIgnoreCase(fieldName)) { if (!jsonObject.isNull(key1)) return jsonObject.getString(key1); else return null; } } } catch (JSONException e) { AdVideoApplication.logger.e(LOG_TAG + "::getString", "Error getting field from JSON: " + e.getMessage()); } return null; }
From source file:de.jackwhite20.japs.client.sub.impl.SubscriberImpl.java
@SuppressWarnings("unchecked") @Override/*www . j av a 2 s . com*/ public void received(JSONObject jsonObject) { String channel = ((String) jsonObject.remove("ch")); if (channel == null || channel.isEmpty()) { return; } HandlerInfo handlerInfo = handlers.get(channel); if (handlerInfo != null) { if (handlerInfo.classType() == ClassType.JSON) { handlerInfo.messageHandler().onMessage(channel, jsonObject); } else { handlerInfo.messageHandler().onMessage(channel, gson.fromJson(jsonObject.toString(), handlerInfo.clazz())); } } else { MultiHandlerInfo multiHandlerInfo = multiHandlers.get(channel); if (multiHandlerInfo != null) { //noinspection Convert2streamapi for (MultiHandlerInfo.Entry entry : multiHandlerInfo.entries()) { if (!jsonObject.isNull(entry.key().value())) { if (jsonObject.get(entry.key().value()).equals(entry.value().value())) { // Remove matched key value pair jsonObject.remove(entry.key().value()); if (entry.classType() == ClassType.JSON) { try { // Invoke the matching method entry.method().invoke(multiHandlerInfo.object(), jsonObject); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } } else { try { // Deserialize with gson entry.method().invoke(multiHandlerInfo.object(), gson.fromJson(jsonObject.toString(), entry.paramClass())); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } } } } } } } }
From source file:com.cssweb.android.view.KlineMini.java
public void initData(JSONObject quoteData) throws JSONException { if (!quoteData.isNull("K")) { this.quoteData = quoteData; this.actualDataLen = quoteData.getJSONArray("K").length(); if (quoteData.isNull("joTMP") && actualDataLen > 1) {//temp?? Log.i(">>>>>temp??K??1>>>>>>", ">>>>>>>>>>>>>>>>>>>>" + quoteData); this.actualDataLen = quoteData.getJSONArray("K").length() - 1; //isTrackNumber = this.actualDataLen - 1; } else {//from w w w . j a v a 2 s .c o m //isTrackNumber = this.actualDataLen - 1; makeTodayData(); } } }
From source file:com.jennifer.ui.chart.ChartBuilder.java
private JSONArray createBrushData(Object brush, JSONArray series_list) { JSONArray list = new JSONArray(); if (brush != null) { if (brush instanceof String) { JSONObject o = new JSONObject(); o.put("type", brush); list.put(o);// w w w . ja v a 2 s .c o m } else if (brush instanceof JSONObject) { list.put(brush); } else if (brush instanceof JSONObject) { list.put(JSONUtil.clone((JSONObject) brush)); } else if (brush instanceof JSONArray) { list = (JSONArray) brush; } else if (brush instanceof JSONArray) { list = JSONUtil.clone((JSONArray) brush); } for (int i = 0, len = list.length(); i < len; i++) { JSONObject b = list.getJSONObject(i); if (b.isNull("target")) { b.put("target", series_list); } else if (b.get("target") instanceof String) { b.put("target", new JSONArray().put(b.getString("target"))); } } } return list; }