List of usage examples for org.json JSONArray optJSONObject
public JSONObject optJSONObject(int index)
From source file:com.androidquery.simplefeed.fragments.FeedFragment.java
public void notiCb(String url, JSONObject jo, AjaxStatus status) { AQUtility.debug("noti", jo); if (jo != null) { int count = 0; PQuery aq = aq2.recycle(header); /*/* w w w .ja va 2 s .c om*/ JSONObject sum = jo.optJSONObject("summary"); if(sum != null){ count = sum.optInt("unseen_count", 0); }*/ JSONArray ja = jo.optJSONArray("data"); for (int i = 0; i < ja.length(); i++) { JSONObject noti = ja.optJSONObject(i); if (noti.optInt("unread", 0) != 0) { count++; } } String message = count + " " + getString(R.string.n_notifications); aq.id(R.id.text_noti).text(message); int colorId = R.color.noti; int tf = Typeface.BOLD; if (count == 0) { colorId = R.color.grey; tf = Typeface.NORMAL; } aq.textColor(getResources().getColor(colorId)).getTextView().setTypeface(null, tf); } }
From source file:org.b3log.xiaov.service.TuringQueryService.java
/** * Chat with Turing Robot./*from w w w . j a v a2 s. com*/ * * @param userName the specified user name * @param msg the specified message * @return robot returned message, return {@code null} if not found */ public String chat(final String userName, String msg) { if (StringUtils.isBlank(msg)) { return null; } if (msg.startsWith(XiaoVs.QQ_BOT_NAME + " ")) { msg = msg.replace(XiaoVs.QQ_BOT_NAME + " ", ""); } if (msg.startsWith(XiaoVs.QQ_BOT_NAME + "")) { msg = msg.replace(XiaoVs.QQ_BOT_NAME + "", ""); } if (msg.startsWith(XiaoVs.QQ_BOT_NAME + ",")) { msg = msg.replace(XiaoVs.QQ_BOT_NAME + ",", ""); } if (msg.startsWith(XiaoVs.QQ_BOT_NAME)) { msg = msg.replace(XiaoVs.QQ_BOT_NAME, ""); } if (StringUtils.isBlank(userName) || StringUtils.isBlank(msg)) { return null; } final HTTPRequest request = new HTTPRequest(); request.setRequestMethod(HTTPRequestMethod.POST); try { request.setURL(new URL(TURING_API)); final String body = "key=" + URLEncoder.encode(TURING_KEY, "UTF-8") + "&info=" + URLEncoder.encode(msg, "UTF-8") + "&userid=" + URLEncoder.encode(userName, "UTF-8"); request.setPayload(body.getBytes("UTF-8")); final HTTPResponse response = URL_FETCH_SVC.fetch(request); final JSONObject data = new JSONObject(new String(response.getContent(), "UTF-8")); final int code = data.optInt("code"); switch (code) { case 40001: case 40002: case 40007: LOGGER.log(Level.ERROR, data.optString("text")); return null; case 40004: return "??~"; case 100000: return data.optString("text"); case 200000: return data.optString("text") + " " + data.optString("url"); case 302000: String ret302000 = data.optString("text") + " "; final JSONArray list302000 = data.optJSONArray("list"); final StringBuilder builder302000 = new StringBuilder(); for (int i = 0; i < list302000.length(); i++) { final JSONObject news = list302000.optJSONObject(i); builder302000.append(news.optString("article")).append(news.optString("detailurl")) .append("\n\n"); } return ret302000 + " " + builder302000.toString(); case 308000: String ret308000 = data.optString("text") + " "; final JSONArray list308000 = data.optJSONArray("list"); final StringBuilder builder308000 = new StringBuilder(); for (int i = 0; i < list308000.length(); i++) { final JSONObject news = list308000.optJSONObject(i); builder308000.append(news.optString("name")).append(news.optString("detailurl")).append("\n\n"); } return ret308000 + " " + builder308000.toString(); default: LOGGER.log(Level.WARN, "Turing Robot default return [" + data.toString(4) + "]"); } } catch (final Exception e) { LOGGER.log(Level.ERROR, "Chat with Turing Robot failed", e); } return null; }
From source file:com.phonegap.ContactManager.java
/** * Executes the request and returns PluginResult. * /*from ww w . j a v a 2s . c o m*/ * @param action The action to execute. * @param args JSONArry of arguments for the plugin. * @param callbackId The callback id used when calling back into JavaScript. * @return A PluginResult object with a status and message. */ public PluginResult execute(String action, JSONArray args, String callbackId) { if (contactAccessor == null) { contactAccessor = ContactAccessor.getInstance(webView, ctx); } PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("search")) { JSONArray res = contactAccessor.search(args.getJSONArray(0), args.optJSONObject(1)); return new PluginResult(status, res, "navigator.service.contacts.cast"); } else if (action.equals("save")) { return new PluginResult(status, contactAccessor.save(args.getJSONObject(0))); } else if (action.equals("remove")) { if (contactAccessor.remove(args.getString(0))) { return new PluginResult(status, result); } else { JSONObject r = new JSONObject(); r.put("code", 2); return new PluginResult(PluginResult.Status.ERROR, r); } } return new PluginResult(status, result); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); return new PluginResult(PluginResult.Status.JSON_EXCEPTION); } }
From source file:org.official.json.CDL.java
/** * Produce a comma delimited text from a JSONArray of JSONObjects. The * first row will be a list of names obtained by inspecting the first * JSONObject.// w ww. ja v a 2 s. co m * @param ja A JSONArray of JSONObjects. * @return A comma delimited text. * @throws JSONException */ public static String toString(JSONArray ja) throws JSONException { JSONObject jo = ja.optJSONObject(0); if (jo != null) { JSONArray names = jo.names(); if (names != null) { return rowToString(names) + toString(names, ja); } } return null; }
From source file:org.official.json.CDL.java
/** * Produce a comma delimited text from a JSONArray of JSONObjects using * a provided list of names. The list of names is not included in the * output./*from w w w. ja v a2 s . c o m*/ * @param names A JSONArray of strings. * @param ja A JSONArray of JSONObjects. * @return A comma delimited text. * @throws JSONException */ public static String toString(JSONArray names, JSONArray ja) throws JSONException { if (names == null || names.length() == 0) { return null; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < ja.length(); i += 1) { JSONObject jo = ja.optJSONObject(i); if (jo != null) { sb.append(rowToString(jo.toJSONArray(names))); } } return sb.toString(); }
From source file:com.facebook.internal.FacebookRequestErrorClassification.java
private static Map<Integer, Set<Integer>> parseJSONDefinition(JSONObject definition) { JSONArray itemsArray = definition.optJSONArray("items"); if (itemsArray.length() == 0) { return null; }//from w ww. j a va2s . com Map<Integer, Set<Integer>> items = new HashMap<>(); for (int i = 0; i < itemsArray.length(); i++) { JSONObject item = itemsArray.optJSONObject(i); if (item == null) { continue; } int code = item.optInt("code"); if (code == 0) { continue; } Set<Integer> subcodes = null; JSONArray subcodesArray = item.optJSONArray("subcodes"); if (subcodesArray != null && subcodesArray.length() > 0) { subcodes = new HashSet<>(); for (int j = 0; j < subcodesArray.length(); j++) { int subCode = subcodesArray.optInt(j); if (subCode != 0) { subcodes.add(subCode); } } } items.put(code, subcodes); } return items; }
From source file:com.facebook.internal.FacebookRequestErrorClassification.java
public static FacebookRequestErrorClassification createFromJSON(JSONArray jsonArray) { if (jsonArray == null) { return null; }/*from w w w. j a va 2s . c om*/ Map<Integer, Set<Integer>> otherErrors = null; Map<Integer, Set<Integer>> transientErrors = null; Map<Integer, Set<Integer>> loginRecoverableErrors = null; String otherRecoveryMessage = null; String transientRecoveryMessage = null; String loginRecoverableRecoveryMessage = null; for (int i = 0; i < jsonArray.length(); i++) { JSONObject definition = jsonArray.optJSONObject(i); if (definition == null) { continue; } String name = definition.optString(KEY_NAME); if (name == null) { continue; } if (name.equalsIgnoreCase(KEY_OTHER)) { otherRecoveryMessage = definition.optString(KEY_RECOVERY_MESSAGE, null); otherErrors = parseJSONDefinition(definition); } else if (name.equalsIgnoreCase(KEY_TRANSIENT)) { transientRecoveryMessage = definition.optString(KEY_RECOVERY_MESSAGE, null); transientErrors = parseJSONDefinition(definition); } else if (name.equalsIgnoreCase(KEY_LOGIN_RECOVERABLE)) { loginRecoverableRecoveryMessage = definition.optString(KEY_RECOVERY_MESSAGE, null); loginRecoverableErrors = parseJSONDefinition(definition); } } return new FacebookRequestErrorClassification(otherErrors, transientErrors, loginRecoverableErrors, otherRecoveryMessage, transientRecoveryMessage, loginRecoverableRecoveryMessage); }
From source file:com.footprint.cordova.plugin.localnotification.LocalNotification.java
@Override public boolean execute(String action, final JSONArray args, final CallbackContext command) throws JSONException { if (action.equalsIgnoreCase("add")) { cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject arguments = args.optJSONObject(0); Options options = new Options(context).parse(arguments); persist(options.getId(), args); add(options, true);/* w ww . j a v a 2 s. com*/ } }); } if (action.equalsIgnoreCase("cancel")) { cordova.getThreadPool().execute(new Runnable() { public void run() { String id = args.optString(0); cancel(id); unpersist(id); command.success(); } }); } if (action.equalsIgnoreCase("cancelAll")) { cordova.getThreadPool().execute(new Runnable() { public void run() { cancelAll(); unpersistAll(); command.success(); } }); } if (action.equalsIgnoreCase("isScheduled")) { String id = args.optString(0); isScheduled(id, command); } if (action.equalsIgnoreCase("getScheduledIds")) { getScheduledIds(command); } if (action.equalsIgnoreCase("deviceready")) { cordova.getThreadPool().execute(new Runnable() { public void run() { deviceready(); } }); } if (action.equalsIgnoreCase("pause")) { isInBackground = true; } if (action.equalsIgnoreCase("resume")) { isInBackground = false; } return true; }
From source file:com.richtodd.android.quiltdesign.block.Quilt.java
static final Quilt createFromJSONObject(JSONObject jsonObject) throws JSONException { int rowCount = jsonObject.optInt("rowCount", 0); int columnCount = jsonObject.optInt("columnCount", 0); float width = (float) jsonObject.optDouble("width", 0); float height = (float) jsonObject.optDouble("height", 0); Quilt quilt = new Quilt(rowCount, columnCount, width, height); if (jsonObject.has("quiltBlocks")) { JSONArray jsonQuiltBlocks = jsonObject.getJSONArray("quiltBlocks"); int index = -1; for (int row = 0; row < quilt.m_rowCount; ++row) { for (int column = 0; column < quilt.m_columnCount; ++column) { index += 1;/* w w w .j a v a 2 s .com*/ if (index < jsonQuiltBlocks.length()) { JSONObject jsonQuiltBlock = jsonQuiltBlocks.optJSONObject(index); if (jsonQuiltBlock != null) { QuiltBlock quiltBlock = QuiltBlock.createFromJSONObject(jsonQuiltBlock); quilt.setQuiltBlock(row, column, quiltBlock); } } } } } quilt.m_new = false; return quilt; }
From source file:com.claude.sharecam.util.CountryMaster.java
private CountryMaster(Context context) { mContext = context;//from w w w .j av a2 s . co m Resources res = mContext.getResources(); // builds country data from json InputStream is = res.openRawResource(R.raw.countries); Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } String jsonString = writer.toString(); JSONArray json = new JSONArray(); try { json = new JSONArray(jsonString); } catch (JSONException e) { e.printStackTrace(); } mCountryList = new String[json.length()]; for (int i = 0; i < json.length(); i++) { JSONObject node = json.optJSONObject(i); if (node != null) { Country country = new Country(); country.mCountryIso = node.optString("iso"); country.mDialPrefix = node.optString("tel"); country.mCountryName = getCountryName(node.optString("iso")); mCountries.add(country); mCountryList[i] = country.mCountryIso; } } }