List of usage examples for org.json JSONArray opt
public Object opt(int index)
From source file:com.facebook.android.friendsmash.integration.GraphAPICall.java
/** * Adds data from specified GraphResponse to stored graphResponse variable. This is used to * combine the data from multiple pages into single GraphResponse object. * See https://developers.facebook.com/docs/graph-api/using-graph-api/#paging for more details * on paging./* ww w . j a v a2 s .c o m*/ */ private void addDataToResponse(GraphResponse response) { if (graphResponse == null) { graphResponse = response; } else { JSONArray newData = response.getJSONObject().optJSONArray("data"); JSONArray existingData = graphResponse.getJSONObject().optJSONArray("data"); for (int i = 0; i < newData.length(); i++) { existingData.put(newData.opt(i)); } } }
From source file:com.adf.bean.AbsBean.java
/** * IMPORT//from w ww . j av a 2 s . c o m * */ private void setArrayColumn(String col, JSONArray arr) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException { Field f; f = getClass().getDeclaredField(col); f.setAccessible(true); Class<?> arrCls = f.getType(); Class<?> objCls = getArrayObjectClass(arrCls); if (objCls != null) { Object array = Array.newInstance(objCls, arr.length()); for (int i = 0; i < arr.length(); i++) { Object oi = arr.opt(i); if (oi.getClass() == objCls) { Array.set(array, i, arr.opt(i)); } else { Constructor<?> cons; try { cons = objCls.getDeclaredConstructor(String.class); cons.setAccessible(true); Object obj = cons.newInstance(oi.toString()); Array.set(array, i, obj); } catch (NoSuchMethodException e) { LogUtil.err("setArrayColumn NoSuchMethodException, col " + col + " :" + e.getMessage()); } catch (InstantiationException e) { LogUtil.err("setArrayColumn InstantiationException, col " + col + " :" + e.getMessage()); } catch (InvocationTargetException e) { LogUtil.err("setArrayColumn InvocationTargetException, col " + col + " :" + e.getMessage()); } } } f.set(this, array); } else { throw new IllegalArgumentException("Can not get Array Column Object class"); } }
From source file:se.chalmers.watchme.utils.MovieHelper.java
/** * Convert a JSONArray to a List/*from w w w. j a v a 2 s . co m*/ * * @param json The JSONArray to convert * @return A List with the generic types specified */ @SuppressWarnings("unchecked") public static <T> List<T> jsonArrayToList(JSONArray json) { List<T> list = new ArrayList<T>(); for (int i = 0; i < json.length(); i++) { list.add((T) json.opt(i)); } return list; }
From source file:com.justone.android.main.MainActivity.java
/*** * //w ww . j a va 2 s. c om * @throws JSONException */ public void checkVersion() throws JSONException { if (!Utils.isWifi(this)) return; String versionInfo = this.dataOp.getUpdateVersionInfo(); JSONObject jsonOb = new JSONObject(versionInfo); String viewData = jsonOb.getString("data"); JSONArray jsonArray = new JSONArray(viewData); for (int i = 0; i < jsonArray.length(); i++) { JSONArray tempJson = (JSONArray) jsonArray.opt(i); if (tempJson.optString(0).equals("version_id")) { JustOne.setServerVersion(tempJson.optInt(1)); } if (tempJson.optString(0).equals("version_name")) JustOne.versionName = tempJson.optString(1); if (tempJson.optString(0).equals("version_desc")) JustOne.setVersionDesc(tempJson.optString(1)); if (tempJson.optString(0).equals("download_href")) JustOne.download_href = tempJson.optString(1); /*ArrayList<String> tempArray = new ArrayList<String>(); tempJson.getJSONArray(index) tempArray.add(String.valueOf(nameToIdMap(tempJson .getString(0)))); tempArray.add(String.valueOf(tempJson.getString(1))); tempArray.add(String.valueOf(tempJson.getString(2))); tempResult.add(tempArray);*/ } if (JustOne.getLocalVersion() < JustOne.getServerVersion()) { // AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("").setMessage(JustOne.getVersionDesc()) .setPositiveButton("", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent updateIntent = new Intent(MainActivity.this, UpdateService.class); updateIntent.putExtra("app_name", getResources().getString(R.string.app_name)); startService(updateIntent); dialog.dismiss(); } }).setNegativeButton("", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alert.create().show(); } }
From source file:com.justone.android.main.MainActivity.java
private ArrayList<ArrayList<String>> loadData(String viewName, String data) throws JSONException { viewName = "data"; JSONObject jsonOb = new JSONObject(data); ArrayList<ArrayList<String>> tempResult = new ArrayList<ArrayList<String>>(); try {/*from ww w . j av a 2 s. c om*/ String viewData = jsonOb.getString(viewName); JSONArray jsonArray = new JSONArray(viewData); for (int i = 0; i < jsonArray.length(); i++) { JSONArray tempJson = (JSONArray) jsonArray.opt(i); ArrayList<String> tempArray = new ArrayList<String>(); tempArray.add(String.valueOf(nameToIdMap(tempJson.getString(0)))); tempArray.add(String.valueOf(tempJson.getString(1))); tempArray.add(String.valueOf(tempJson.getString(2))); tempResult.add(tempArray); } } catch (JSONException e) { e.printStackTrace(); } String id = jsonOb.getString("id"); ArrayList<String> tempArray = new ArrayList<String>(); tempArray.add(String.valueOf(nameToIdMap(jsonOb.getString("type") + "_id"))); tempArray.add(id); tempArray.add("text"); tempResult.add(tempArray); return tempResult; }
From source file:com.clover.sdk.v3.JsonHelper.java
public static List toList(JSONArray array) { List list = new ArrayList(); for (int i = 0; i < array.length(); i++) { list.add(fromJson(array.opt(i))); }//from www .jav a 2s .com return list; }
From source file:com.vk.sdkweb.api.model.ParseUtils.java
/** * Parses array from given JSONArray./*from ww w.j a v a2s . c om*/ * Supports parsing of primitive types and {@link com.vk.sdkweb.api.model.VKApiModel} instances. * @param array JSONArray to parse * @param arrayClass type of array field in class. * @return object to set to array field in class * @throws JSONException if given array have incompatible type with given field. */ private static Object parseArrayViaReflection(JSONArray array, Class arrayClass) throws JSONException { Object result = Array.newInstance(arrayClass.getComponentType(), array.length()); Class<?> subType = arrayClass.getComponentType(); for (int i = 0; i < array.length(); i++) { try { Object item = array.opt(i); if (VKApiModel.class.isAssignableFrom(subType) && item instanceof JSONObject) { VKApiModel model = (VKApiModel) subType.newInstance(); item = model.parse((JSONObject) item); } Array.set(result, i, item); } catch (InstantiationException e) { throw new JSONException(e.getMessage()); } catch (IllegalAccessException e) { throw new JSONException(e.getMessage()); } catch (IllegalArgumentException e) { throw new JSONException(e.getMessage()); } } return result; }
From source file:at.alladin.rmbt.android.util.CheckIpTask.java
@Override protected JSONArray doInBackground(final Void... params) { needsRetry = false;/* ww w . j a v a 2s . c om*/ serverConn = new ControlServerConnection(activity); try { Socket s = new Socket(); InetSocketAddress addr = new InetSocketAddress( ConfigHelper.getCachedControlServerNameIpv4(activity.getApplicationContext()), ConfigHelper.getControlServerPort(activity.getApplicationContext())); s.connect(addr, 5000); privateIpv4 = s.getLocalAddress(); s.close(); } catch (Exception e) { e.printStackTrace(); } try { Socket s = new Socket(); InetSocketAddress addr = new InetSocketAddress( ConfigHelper.getCachedControlServerNameIpv6(activity.getApplicationContext()), ConfigHelper.getControlServerPort(activity.getApplicationContext())); s.connect(addr, 5000); privateIpv6 = s.getLocalAddress(); s.close(); } catch (SocketTimeoutException e) { e.printStackTrace(); needsRetry = ConfigHelper.isRetryRequiredOnIpv6SocketTimeout(activity); } catch (Exception e) { needsRetry = false; e.printStackTrace(); } newsList = new JSONArray(); if (privateIpv4 != null) { JSONArray response = serverConn.requestIp(false); if (response != null && response.length() >= 1) { newsList.put(response.opt(0)); } } else { Log.d(DEBUG_TAG, "no private ipv4 found"); } if (privateIpv6 != null) { JSONArray response = serverConn.requestIp(true); if (response != null && response.length() >= 1) { newsList.put(response.opt(0)); } } else { Log.d(DEBUG_TAG, "no private ipv6 found"); } return newsList; }
From source file:org.official.json.JSONML.java
/** * Reverse the JSONML transformation, making an XML text from a JSONArray. * @param ja A JSONArray./*from w w w .j a v a2 s. co m*/ * @return An XML string. * @throws JSONException */ public static String toString(JSONArray ja) throws JSONException { int i; JSONObject jo; String key; Iterator<String> keys; int length; Object object; StringBuilder sb = new StringBuilder(); String tagName; String value; // Emit <tagName tagName = ja.getString(0); XML.noSpace(tagName); tagName = XML.escape(tagName); sb.append('<'); sb.append(tagName); object = ja.opt(1); if (object instanceof JSONObject) { i = 2; jo = (JSONObject) object; // Emit the attributes keys = jo.keys(); while (keys.hasNext()) { key = keys.next(); XML.noSpace(key); value = jo.optString(key); if (value != null) { sb.append(' '); sb.append(XML.escape(key)); sb.append('='); sb.append('"'); sb.append(XML.escape(value)); sb.append('"'); } } } else { i = 1; } // Emit content in body length = ja.length(); if (i >= length) { sb.append('/'); sb.append('>'); } else { sb.append('>'); do { object = ja.get(i); i += 1; if (object != null) { if (object instanceof String) { sb.append(XML.escape(object.toString())); } else if (object instanceof JSONObject) { sb.append(toString((JSONObject) object)); } else if (object instanceof JSONArray) { sb.append(toString((JSONArray) object)); } } } while (i < length); sb.append('<'); sb.append('/'); sb.append(tagName); sb.append('>'); } return sb.toString(); }
From source file:org.jjdltc.cordova.plugin.sftp.asyncSFTPAction.java
private void actionExecution(JSONArray actions) throws SftpException { this.fileListSize = actions.length(); for (int i = 0; i < actions.length(); i++) { JSONObject item = (JSONObject) actions.opt(i); if (action == "download") { this.sftpChannel.get(item.optString("remote"), item.optString("local"), new progressMonitor(this.actualWv)); } else {/*from www .ja va 2 s . c o m*/ this.sftpChannel.put(item.optString("local"), item.optString("remote"), new progressMonitor(this.actualWv), ChannelSftp.OVERWRITE); } this.publishProgress(i + 1); } }