List of usage examples for org.json JSONArray get
public Object get(int index) throws JSONException
From source file:org.jabsorb.ng.serializer.impl.ListSerializer.java
@Override public ObjectMatch tryUnmarshall(final SerializerState state, final Class<?> clazz, final Object o) throws UnmarshallException { final JSONObject jso = (JSONObject) o; String java_class; // Hint presence try {/*from www. j a va 2s.c om*/ java_class = jso.getString("javaClass"); } catch (final JSONException e) { throw new UnmarshallException("Could not read javaClass", e); } if (java_class == null) { throw new UnmarshallException("no type hint"); } // Class compatibility check if (!classNameCheck(java_class)) { throw new UnmarshallException("not a List"); } // JSON Format check JSONArray jsonlist; try { jsonlist = jso.getJSONArray("list"); } catch (final JSONException e) { throw new UnmarshallException("Could not read list: " + e.getMessage(), e); } if (jsonlist == null) { throw new UnmarshallException("list missing"); } // Content check final ObjectMatch m = new ObjectMatch(-1); state.setSerialized(o, m); int idx = 0; try { for (idx = 0; idx < jsonlist.length(); idx++) { m.setMismatch(ser.tryUnmarshall(state, null, jsonlist.get(idx)).max(m).getMismatch()); } } catch (final UnmarshallException e) { throw new UnmarshallException("element " + idx + " " + e.getMessage(), e); } catch (final JSONException e) { throw new UnmarshallException("element " + idx + " " + e.getMessage(), e); } return m; }
From source file:org.jabsorb.ng.serializer.impl.ListSerializer.java
@Override public Object unmarshall(final SerializerState state, final Class<?> clazz, final Object o) throws UnmarshallException { final JSONObject jso = (JSONObject) o; String java_class; // Hint check try {/* w w w . j a va2 s .com*/ java_class = jso.getString("javaClass"); } catch (final JSONException e) { throw new UnmarshallException("Could not read javaClass", e); } if (java_class == null) { throw new UnmarshallException("no type hint"); } // Create the list final AbstractList<Object> ablist; if (java_class.equals("java.util.List") || java_class.equals("java.util.AbstractList") || java_class.equals("java.util.ArrayList")) { ablist = new ArrayList<Object>(); } else if (java_class.equals("java.util.LinkedList")) { ablist = new LinkedList<Object>(); } else if (java_class.equals("java.util.Vector")) { ablist = new Vector<Object>(); } else { throw new UnmarshallException("not a List"); } // Parse the JSON list JSONArray jsonlist; try { jsonlist = jso.getJSONArray("list"); } catch (final JSONException e) { throw new UnmarshallException("Could not read list: " + e.getMessage(), e); } if (jsonlist == null) { throw new UnmarshallException("list missing"); } state.setSerialized(o, ablist); int idx = 0; try { for (idx = 0; idx < jsonlist.length(); idx++) { ablist.add(ser.unmarshall(state, null, jsonlist.get(idx))); } } catch (final UnmarshallException e) { throw new UnmarshallException("element " + idx + " " + e.getMessage(), e); } catch (final JSONException e) { throw new UnmarshallException("element " + idx + " " + e.getMessage(), e); } return ablist; }
From source file:org.cohorte.remote.dispatcher.beans.ParseUtils.java
/** * Converts a JSON array into a list/*from w w w. j a va 2s .c o m*/ * * @param aJSONArray * A JSON array or null * @return A list, or null * @throws JSONException * Error parsing the JSON object */ public static List<Object> jsonToList(final JSONArray aJSONArray) throws JSONException { if (aJSONArray == null) { // Nothing to do return null; } // Prepare the list final List<Object> content = new LinkedList<Object>(); for (int i = 0; i < aJSONArray.length(); i++) { // Get the next object final Object rawObject = aJSONArray.get(i); if (rawObject instanceof JSONObject) { // Got a child map content.add(jsonToMap((JSONObject) rawObject)); } else if (rawObject instanceof JSONArray) { // Got a child collection content.add(jsonToList((JSONArray) rawObject)); } else { // Use the read value content.add(rawObject); } } return content; }
From source file:org.upv.satrd.fic2.fe.connectors.citySDK.Collector.java
public JSONArray getPOIArrayByCategory(Connection con, String category, String city, String bbox, Integer limit, Logger log) {/*from w ww . ja v a 2 s . c o m*/ if (log == null) log = Collector.log; JSONArray returnedPois = new JSONArray(); try { // get the corresponding category in the source String projectedCategory = getProjectedCategory(con, category, city, log); if ((projectedCategory == null) || (projectedCategory.isEmpty())) { log.error("Error. Collector.getPOIArrayByCategory(). No matching category in source " + source.getName() + " for category " + category); projectedCategory = " "; } else { //Check if there is one or more categories mapping (e.g, category 'shop' maps in OSM to several categories:mall, book_shop, supermarket,etc.) String[] projectedCategoryArray = projectedCategory.split(","); int cat_length = projectedCategoryArray.length; JSONArray selectedPois = new JSONArray(); if (cat_length > 1) { JSONObject listPois = new JSONObject(); //There are more than one category to map log.debug("Collector.getPOIArrayByCategory(). Category '" + category + "' maps to " + cat_length + " different categories. Building an aggregated JSON file"); for (int h = 0; h < cat_length; h++) { String current_projectedCategory = projectedCategoryArray[h]; log.debug("Collector.getPOIArrayByCategory(). ProjectedCategory " + h + ": " + current_projectedCategory); JSONObject listPois_aux = CommonUtils.getPOIListBySourceAndCategory(source.getUrlaccess(), source.getName(), current_projectedCategory, bbox, limit.toString(), city, log); if (h == 0) { //Take the first result and store it listPois = listPois_aux; selectedPois = listPois.getJSONArray("poi"); } else { //Append/Merge the new found POIs in listPois JSONArray array_aux = listPois_aux.getJSONArray("poi"); for (int g = 0; g < array_aux.length(); g++) { Object obj = array_aux.get(g); selectedPois.put(obj); } } } } else { log.debug("Collector.getPOIArrayByCategory(). Corresponding category for '" + category + "' in source " + source.getName() + " : " + projectedCategory); JSONObject listPois = CommonUtils.getPOIListBySourceAndCategory(source.getUrlaccess(), source.getName(), projectedCategory, bbox, limit.toString(), city, log); //Get the array of the retrieved object selectedPois = listPois.getJSONArray("poi"); } //TODO :this information may be stored for statistics after the fusion has taken place log.debug("Collector.getPOIArrayByCategory(). Length " + source.getName() + " : " + selectedPois.length()); returnedPois = cleanPOIJSONArray(con, selectedPois, projectedCategory, log); } } catch (Exception ex) { System.out.println("Error Collector.getPOIArrayByCategory() :" + ex.getMessage()); log.error("Error Collector.getPOIArrayByCategory() :" + ex.getMessage()); } return returnedPois; }
From source file:org.springsource.ide.eclipse.commons.ui.tips.TipProvider.java
public void refresh() { error = null;//from www . j a v a 2 s . co m InputStreamReader tipReader = new InputStreamReader(TipProvider.class.getClassLoader() .getResourceAsStream("org/springsource/ide/eclipse/commons/ui/tips/spring_tool_tips.json")); JSONTokener tokener = new JSONTokener(tipReader); List<TipInfo> tipList = Collections.emptyList(); try { JSONArray json = new JSONArray(tokener); int length = json.length(); tipList = new ArrayList<TipInfo>(length); for (int i = 0; i < length; i++) { JSONObject object = (JSONObject) json.get(i); if (object.has("required")) { String requiredPlugin = object.getString("required"); if (Platform.getBundle(requiredPlugin) == null) { continue; } } tipList.add(new TipInfo(object.getString("infoText"), object.getString("linkText"), object.has("keyBindingId") ? object.getString("keyBindingId") : null)); } } catch (JSONException e) { CorePlugin.log("Error parsing tips of the day file", e); error = e; } if (tipList.size() == 0) { tips = new TipInfo[] { new TipInfo("There's a lot going on with Spring", "Read the latest <a href\"http://spring.io/blog\">Spring news</a>.") }; } else { tips = tipList.toArray(new TipInfo[tipList.size()]); } pointer = RAND.nextInt(tips.length); }
From source file:com.googlecode.mashups.services.unittests.FeedReaderServiceTest.java
public void testGetJSONFeedFromGoogle() throws Exception { try {/*w w w .jav a 2s . co m*/ String feedURL = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=javaone"; JSONArray resultArray = (JSONArray) GenericServicesFactory.getFeedReaderService().readJSONFeed(feedURL, "results"); System.out.println("Trying to search for JavaOne in Google ..."); for (int i = 0; i < resultArray.length(); ++i) { JSONObject jsonObject = (JSONObject) resultArray.get(i); System.out.println( "Title: \"" + jsonObject.get("titleNoFormatting") + "\", URL: " + jsonObject.get("url")); } } catch (Exception e) { e.printStackTrace(); fail("Unable to execute the JSON Feed Reader ..."); } }
From source file:com.googlecode.mashups.services.unittests.FeedReaderServiceTest.java
public void testGetJSONFeedFromDelicious() throws Exception { try {/*from w ww. j a v a 2 s.com*/ String feedURL = "http://feeds.delicious.com/v2/json/popular/javaone"; JSONArray resultArray = (JSONArray) GenericServicesFactory.getFeedReaderService().readJSONFeed(feedURL, "none"); System.out.println("Trying to search for popular 'JavaOne' tags in Delicious ..."); for (int i = 0; i < resultArray.length(); ++i) { JSONObject jsonObject = (JSONObject) resultArray.get(i); System.out.println("Title: \"" + jsonObject.get("d") + "\", URL: " + jsonObject.get("u")); } } catch (Exception e) { e.printStackTrace(); fail("Unable to execute the JSON Feed Reader ..."); } }
From source file:juserdefaults.JUserDefaults.java
public Object[] getArray(String key) { JSONArray jsonArray = getKeyValueStore().getJSONArray(key); Object[] a = new Object[jsonArray.length()]; for (int i = 0; i < jsonArray.length(); i++) { a[i] = jsonArray.get(i); }//from www . j a v a2 s . c o m return a; }
From source file:com.thoughtworks.xstream.io.json.JsonWriterModeTest.java
private static boolean equals(JSONArray array1, JSONArray array2) { int length = array1.length(); if (length == array2.length()) { try {/*from w w w .j a v a2 s. co m*/ while (length-- > 0) { if (!equals(array1.get(length), array2.get(length))) { return false; } } return true; } catch (JSONException e) { // ignore - return false } } return false; }
From source file:pt.webdetails.cdv.operations.PushWarningsHandler.java
private static Object escapeStringLiterals(Object value) { if (value instanceof String) { return StringUtils.replace((String) value, "\"", "\\\""); } else if (value instanceof JSONArray) { JSONArray jArray = (JSONArray) value; for (int i = 0; i < jArray.length(); i++) { try { jArray.put(i, escapeStringLiterals(jArray.get(i))); } catch (JSONException e) { logger.error("Error escaping array contents for array " + jArray.toString(), e); }/*from w w w . ja v a2 s . c o m*/ } return jArray; } else return value; }