List of usage examples for org.json.simple JSONArray size
public int size()
From source file:Models.Geographic.Repository.RepositoryGoogle.java
/** * //ww w. j a v a 2 s.c o m * @param latitude * @param longitude * @return */ public static HashMap reverse(double latitude, double longitude) { HashMap a = new HashMap(); try { //URL url=new URL(Configuration.getParameter("geocoding_google_url_send_json") + "latlng=" + String.valueOf(latitude) + "," + String.valueOf(longitude)); URL url = new URL(Configuration.getParameter("geocoding_google_url_send_json") + "latlng=" + String.valueOf(latitude) + "," + String.valueOf(longitude)); URL file_url = new URL( url.getProtocol() + "://" + url.getHost() + signRequest(url.getPath(), url.getQuery())); //BufferedReader lector=new BufferedReader(new InputStreamReader(url.openStream())); BufferedReader lector = new BufferedReader(new InputStreamReader(file_url.openStream())); String textJson = "", tempJs; while ((tempJs = lector.readLine()) != null) textJson += tempJs; if (textJson == null) throw new Exception("Don't found item"); JSONObject google = ((JSONObject) JSONValue.parse(textJson)); a.put("status", google.get("status").toString()); if (a.get("status").toString().equals("OK")) { JSONArray results = (JSONArray) google.get("results"); JSONArray address_components = (JSONArray) ((JSONObject) results.get(2)).get("address_components"); for (int i = 0; i < address_components.size(); i++) { JSONObject items = (JSONObject) address_components.get(i); //if(((JSONObject)types.get(0)).get("").toString().equals("country")) if (items.get("types").toString().contains("country")) { a.put("country", items.get("long_name").toString()); a.put("iso", items.get("short_name").toString()); break; } } } } catch (Exception ex) { a = null; System.out.println("Error Google Geocoding: " + ex); } return a; }
From source file:com.jilk.ros.rosbridge.implementation.JSON.java
private static Object convertJSONArrayToArray(JSONArray ja, Class c, Registry<Class> r) { Object result = Array.newInstance(c, ja.size()); for (int i = 0; i < ja.size(); i++) { Object lookup = ja.get(i); Object value = null;//from w w w . j a v a2s . co m if (lookup != null) { if (lookup.getClass().equals(JSONObject.class)) value = convertJSONObjectToMessage((JSONObject) lookup, c, r); else if (lookup.getClass().equals(JSONArray.class)) // this is not actually allowed in ROS value = convertJSONArrayToArray((JSONArray) lookup, c.getComponentType(), r); else value = convertJSONPrimitiveToPrimitive(lookup, c); Array.set(result, i, value); } } return result; }
From source file:it.cnr.isti.thematrix.scripting.sys.DatasetSchema.java
/** * Convert the argument JSON string into a DatasetSchema * @param jsonContent // w ww .java2s . c o m * @return a DatasetSchema */ public static DatasetSchema fromJSON(String jsonContent) { Object obj = JSONValue.parse(jsonContent); JSONArray array = (JSONArray) obj; String schemaName = (String) array.get(0); DatasetSchema ds = new DatasetSchema(schemaName); for (int i = 1; i < array.size(); i++) { JSONObject o = (JSONObject) array.get(i); String name = (String) o.get("name"); DataType type = DataType.valueOf((String) o.get("type")); ds.put(new Symbol<Integer>(name, null, type)); } return ds; }
From source file:com.worldline.easycukes.commons.helpers.JSONHelper.java
/** * Returns the value of a given property from a particular * {@link JSONObject}/*from ww w . j a v a2 s .com*/ * * @param jsonObject the {@link JSONObject} to be used for extracting values * @param property the property you'd like to get from the specified * {@link JSONObject} * @return the value of the specified property if it's found, or null if it * cannot be found */ public static String getValue(@NonNull final JSONObject jsonObject, @NonNull final String property) { if (jsonObject != null) if (jsonObject.get(property) != null) return jsonObject.get(property).toString(); final int index = property.indexOf("."); if (index > 0) { final Object object = getProperty(jsonObject, property.substring(0, index)); if (object != null) { if (object instanceof JSONObject) return getValue((JSONObject) object, property.substring(index + 1)); if (object instanceof JSONArray) { final JSONArray jsonArray = (JSONArray) object; if (jsonArray.size() > 0) return getValue((JSONObject) jsonArray.get(0), property.substring(index + 1)); } } } return null; }
From source file:com.memetix.mst.MicrosoftTranslatorAPI.java
private static Integer[] jsonToIntArr(final String inputString) throws Exception { final JSONArray jsonArr = (JSONArray) JSONValue.parse(inputString); Integer[] intArr = new Integer[jsonArr.size()]; int i = 0;//w ww. j a v a 2 s . c o m for (Object obj : jsonArr) { intArr[i] = ((Long) obj).intValue(); i++; } return intArr; }
From source file:com.memetix.mst.MicrosoftTranslatorAPI.java
private static String[] jsonToStringArr(final String inputString, final String propertyName) throws Exception { final JSONArray jsonArr = (JSONArray) JSONValue.parse(inputString); String[] values = new String[jsonArr.size()]; int i = 0;//from w w w. j a va 2 s.co m for (Object obj : jsonArr) { if (propertyName != null && propertyName.length() != 0) { final JSONObject json = (JSONObject) obj; if (json.containsKey(propertyName)) { values[i] = json.get(propertyName).toString(); } } else { values[i] = obj.toString(); } i++; } return values; }
From source file:jGPIO.DTO.java
public static JSONObject findDetails(String gpio_name) { // Do we have a valid definition file, or should we just direct map? if (pinDefinitions == null) { autoDetectSystemFile();//from ww w. j a v a2s. co m } if (pinDefinitions == null) { System.out.println("No definitions file found, assuming direct mapping"); return null; } for (Object obj : pinDefinitions) { JSONObject jObj = (JSONObject) obj; String key = (String) jObj.get("key"); if (key.equalsIgnoreCase(gpio_name)) { return jObj; } if (jObj.containsKey("options")) { JSONArray options = (JSONArray) jObj.get("options"); for (int i = 0; i < options.size(); i++) { String option = (String) options.get(i); if (option.equalsIgnoreCase(gpio_name)) { return jObj; } } } } // not found return null; }
From source file:net.itransformers.toplogyviewer.gui.neo4j.Neo4jLoader.java
public static String getLatestNetwork() { String HighestNetworkID = null; String query = "start network=node:node_auto_index(name='network') return network"; String params = ""; String output = executeCypherQuery(query, params); JSONObject json = null;// w w w . j a va 2s . com try { json = (JSONObject) new JSONParser().parse(output); } catch (ParseException e) { e.printStackTrace(); } JSONArray jsonData = (JSONArray) json.get("data"); JSONArray array = (JSONArray) jsonData.get(jsonData.size() - 1); JSONObject object1 = (JSONObject) array.get(array.size() - 1); String delims = "[/]"; //Get the latest element of the URL e.g the node ID. String[] tokens = object1.get("self").toString().split(delims); HighestNetworkID = tokens[tokens.length - 1]; return HighestNetworkID; }
From source file:com.creapple.tms.mobiledriverconsole.utils.MDCUtils.java
/** * Get distance information between two GPS * @param originLat/*from w w w . j a v a 2 s . c o m*/ * @param originLon * @param destLat * @param destLon * @return */ public static String[] getDistanceInfo(double originLat, double originLon, double destLat, double destLon) { String[] infos = new String[] { "0", "0" }; String address = Constants.GOOGLE_DISTANCE_MATRIX_ADDRESS; address += originLat + "," + originLon; address += "&destinations="; address += destLat + "," + destLon; address += "&mode=driving&units=metric&language=en&key="; address += Constants.GOOGLE_DISTANCE_MATRIX_API_KEY; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(address).build(); Response response = null; String dist = null; try { response = client.newCall(request).execute(); dist = response.body().string(); } catch (IOException e) { return infos; } Log.d("@@@@@@", dist); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = null; try { jsonObject = (JSONObject) jsonParser.parse(dist); } catch (ParseException e) { return infos; } // status check as well JSONArray rows = (JSONArray) jsonObject.get(Constants.GOOGLE_DISTANCE_MATRIX_ROWS); for (int i = 0; i < rows.size(); i++) { JSONObject obj = (JSONObject) rows.get(i); JSONArray elements = (JSONArray) obj.get(Constants.GOOGLE_DISTANCE_MATRIX_ELEMENTS); for (int j = 0; j < elements.size(); j++) { JSONObject datas = (JSONObject) elements.get(j); JSONObject distance = (JSONObject) datas.get(Constants.GOOGLE_DISTANCE_MATRIX_DISTANCE); JSONObject duration = (JSONObject) datas.get(Constants.GOOGLE_DISTANCE_MATRIX_DURATION); infos[0] = distance.get(Constants.GOOGLE_DISTANCE_MATRIX_VALUE) + ""; infos[1] = duration.get(Constants.GOOGLE_DISTANCE_MATRIX_TEXT) + ""; } } String status = jsonObject.get(Constants.GOOGLE_DISTANCE_MATRIX_STATUS).toString(); // Log.d("@@@@@@", status); if (!StringUtils.equalsIgnoreCase(Constants.GOOGLE_DISTANCE_MATRIX_OK, status)) { return infos; } return infos; }
From source file:com.worldline.easycukes.commons.helpers.JSONHelper.java
/** * Returns <b>true</b> if JSON array a1 is equals to JSON array a2. * * @param a1 a {@link JSONArray} containing some JSON data * @param a2 another {@link JSONArray} containing some JSON data * @return true if the json arrays are equals *//*from w w w. j a v a 2s .c om*/ public static boolean equals(@NonNull JSONArray a1, @NonNull JSONArray a2) { if (a1 == a2) return true; if (a1.size() != a2.size()) return false; final ListIterator i1 = a1.listIterator(); ListIterator i2; boolean found = false; while (i1.hasNext()) { final Object o1 = i1.next(); found = false; i2 = a2.listIterator(); if (o1 instanceof JSONObject) while (i2.hasNext()) { final Object o2 = i2.next(); if (!(o2 instanceof JSONObject)) return false; if (equals((JSONObject) o1, (JSONObject) o2)) found = true; } else if (o1 instanceof JSONArray) while (i2.hasNext()) { final Object o2 = i2.next(); if (!(o2 instanceof JSONArray)) return false; if (equals((JSONArray) o1, (JSONArray) o2)) found = true; } else while (i2.hasNext()) { final Object o2 = i2.next(); if (o1.equals(o2)) found = true; } if (!found) return false; } return true; }