List of usage examples for org.json JSONArray put
public JSONArray put(Object value)
From source file:org.seadpdt.impl.RepoServicesImpl.java
@GET @Path("/") @Produces(MediaType.APPLICATION_JSON)//from ww w.j a v a 2 s .c o m public Response getRepositoryList() { FindIterable<Document> iter = repositoriesCollection.find(); iter.projection(new Document("orgidentifier", 1).append("repositoryURL", 1).append("repositoryName", 1) .append("lastUpdate", 1).append("_id", 0)); MongoCursor<Document> cursor = iter.iterator(); JSONArray array = new JSONArray(); while (cursor.hasNext()) { array.put(new JSONObject(cursor.next().toJson())); } return Response.ok(array.toString()).cacheControl(control).build(); }
From source file:com.yahoo.sql4d.query.groupby.Having.java
public Map<String, Object> getJsonMap() { Map<String, Object> map = new LinkedHashMap<>(); map.put("type", getTypeForSymbol(type)); map.put("aggregation", aggregation); map.put("value", value); if (havingSpecs != null) { if (type.equals("not")) { map.put("havingSpecs", havingSpecs.get(0).getJson()); } else {//from w w w . j a v a 2s . c o m JSONArray havingSpecsArray = new JSONArray(); for (Having item : havingSpecs) { havingSpecsArray.put(item.getJson()); } map.put("havingSpecs", havingSpecsArray); } } return map; }
From source file:io.github.acashjos.anarch.RegexValueMatchBuilder.java
/** * Internal function/*from w w w. jav a 2s . c om*/ * Processes response body according to the matchbuilder specifications and returns JSONObject * @param responseText String on which the operations are to be conducted * @return JSONObject Object with all the extracted properties */ @Override protected JSONObject processResponseText(String responseText) { JSONObject output = new JSONObject(); for (PatternBlueprint test : patternSet) { Pattern p = Pattern.compile(test.pattern); Matcher m = p.matcher(responseText); JSONArray arr = new JSONArray(); while (m.find()) { Log.v("debug", "find(): " + m.group()); JSONObject single = test.once ? output : new JSONObject(); //insers keys from mainTest for (Map.Entry<String, Integer> outkey : test.outputKeySet.entrySet()) { Log.v("debug", "outputkeyset: " + outkey.getKey()); try { single.put(outkey.getKey(), m.group(outkey.getValue())); } catch (JSONException e) { e.printStackTrace(); continue; } } for (PatternBlueprint subtest : test.subPatternSet) { String patern = test.pattern; for (int i = 1; i <= m.groupCount(); ++i) { patern = patern.replace("%" + i, m.group(i)); } Pattern p2 = Pattern.compile(patern); Matcher m2 = p2.matcher(m.group(subtest.source_group)); if (m2.find()) //insert keys from subtest for (Map.Entry<String, Integer> outkey : subtest.outputKeySet.entrySet()) try { single.put(outkey.getKey(), m2.group(outkey.getValue())); } catch (JSONException e) { e.printStackTrace(); continue; } } if (!test.once) { arr.put(single); } } if (!test.once) try { output.put(test.id, arr); } catch (JSONException e) { continue; } } return output; }
From source file:org.collectionspace.chain.csp.persistence.services.relation.TestRelationsThroughWebapp.java
@Test public void testMultipleCreate() throws Exception { // Create test cataloging ServletTester jetty = tester.setupJetty(); HttpTester out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty); String id1 = out.getHeader("Location"); out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty);/*from w w w . j av a 2 s. c om*/ String id2 = out.getHeader("Location"); out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty); String id3 = out.getHeader("Location"); String[] path1 = id1.split("/"); String[] path2 = id2.split("/"); String[] path3 = id3.split("/"); // Do the rleation JSONObject data1 = createRelation(path3[1], path3[2], "affects", path1[1], path1[2], false); JSONObject data2 = createRelation(path3[1], path3[2], "affects", path2[1], path2[2], false); JSONArray datas = new JSONArray(); datas.put(data1); datas.put(data2); JSONObject data = new JSONObject(); data.put("items", datas); out = tester.POSTData("/relationships", data, jetty); // Check it out = tester.GETData(id3, jetty); JSONObject data3 = new JSONObject(out.getContent()); JSONArray rel3 = data3.getJSONObject("relations").getJSONArray("cataloging"); assertNotNull(rel3); assertEquals(2, rel3.length()); tester.DELETEData(id1, jetty); tester.DELETEData(id2, jetty); tester.DELETEData(id3, jetty); tester.stopJetty(jetty); }
From source file:org.ohmage.reminders.types.location.LocationTrigger.java
private JSONArray getLocations(Context context, int categId) { JSONArray jLocs = new JSONArray(); LocTrigDB db = new LocTrigDB(context); db.open();//from w w w. jav a 2s . c om Cursor cLocs = db.getLocations(categId); if (cLocs.moveToFirst()) { do { int lat = cLocs.getInt(cLocs.getColumnIndexOrThrow(LocTrigDB.KEY_LAT)); int lng = cLocs.getInt(cLocs.getColumnIndexOrThrow(LocTrigDB.KEY_LONG)); float radius = cLocs.getFloat(cLocs.getColumnIndexOrThrow(LocTrigDB.KEY_RADIUS)); JSONObject jLoc = new JSONObject(); try { jLoc.put(KEY_LATITUDE, (lat / 1E6)); jLoc.put(KEY_LONGITUDE, (lng / 1E6)); jLoc.put(KEY_RADIUS, radius); jLocs.put(jLoc); } catch (JSONException e) { Log.e(TAG, "LocationTrigger: Error adding locations to " + "preference JSON", e); } } while (cLocs.moveToNext()); } cLocs.close(); db.close(); return jLocs; }
From source file:org.ohmage.reminders.types.location.LocationTrigger.java
@Override /*//www .ja v a2 s.co m * Returns a JSON object containing all the location * settings. */ public JSONObject getPreferences(Context context) { LocTrigDB db = new LocTrigDB(context); db.open(); JSONArray jPlaces = new JSONArray(); Cursor cCategs = db.getAllCategories(); if (cCategs.moveToFirst()) { do { String categName = cCategs.getString(cCategs.getColumnIndexOrThrow(LocTrigDB.KEY_NAME)); int categId = cCategs.getInt(cCategs.getColumnIndexOrThrow(LocTrigDB.KEY_ID)); JSONObject jPlace = new JSONObject(); try { jPlace.put(KEY_NAME, categName); jPlace.put(KEY_LOCATIONS, getLocations(context, categId)); jPlaces.put(jPlace); } catch (JSONException e) { Log.e(TAG, "LocationTrigger: Error adding place to " + "preference JSON", e); } } while (cCategs.moveToNext()); } cCategs.close(); db.close(); JSONObject jPref = new JSONObject(); try { jPref.put(KEY_PLACES, jPlaces); } catch (JSONException e) { Log.e(TAG, "LocationTrigger: Error adding places to " + "preference JSON", e); } return jPref; }
From source file:com.trk.aboutme.facebook.SharedPreferencesTokenCachingStrategy.java
private void serializeKey(String key, Bundle bundle, SharedPreferences.Editor editor) throws JSONException { Object value = bundle.get(key); if (value == null) { // Cannot serialize null values. return;/*w w w . ja va2s.co m*/ } String supportedType = null; JSONArray jsonArray = null; JSONObject json = new JSONObject(); if (value instanceof Byte) { supportedType = TYPE_BYTE; json.put(JSON_VALUE, ((Byte) value).intValue()); } else if (value instanceof Short) { supportedType = TYPE_SHORT; json.put(JSON_VALUE, ((Short) value).intValue()); } else if (value instanceof Integer) { supportedType = TYPE_INTEGER; json.put(JSON_VALUE, ((Integer) value).intValue()); } else if (value instanceof Long) { supportedType = TYPE_LONG; json.put(JSON_VALUE, ((Long) value).longValue()); } else if (value instanceof Float) { supportedType = TYPE_FLOAT; json.put(JSON_VALUE, ((Float) value).doubleValue()); } else if (value instanceof Double) { supportedType = TYPE_DOUBLE; json.put(JSON_VALUE, ((Double) value).doubleValue()); } else if (value instanceof Boolean) { supportedType = TYPE_BOOLEAN; json.put(JSON_VALUE, ((Boolean) value).booleanValue()); } else if (value instanceof Character) { supportedType = TYPE_CHAR; json.put(JSON_VALUE, value.toString()); } else if (value instanceof String) { supportedType = TYPE_STRING; json.put(JSON_VALUE, (String) value); } else if (value instanceof Enum<?>) { supportedType = TYPE_ENUM; json.put(JSON_VALUE, value.toString()); json.put(JSON_VALUE_ENUM_TYPE, value.getClass().getName()); } else { // Optimistically create a JSONArray. If not an array type, we can null // it out later jsonArray = new JSONArray(); if (value instanceof byte[]) { supportedType = TYPE_BYTE_ARRAY; for (byte v : (byte[]) value) { jsonArray.put((int) v); } } else if (value instanceof short[]) { supportedType = TYPE_SHORT_ARRAY; for (short v : (short[]) value) { jsonArray.put((int) v); } } else if (value instanceof int[]) { supportedType = TYPE_INTEGER_ARRAY; for (int v : (int[]) value) { jsonArray.put(v); } } else if (value instanceof long[]) { supportedType = TYPE_LONG_ARRAY; for (long v : (long[]) value) { jsonArray.put(v); } } else if (value instanceof float[]) { supportedType = TYPE_FLOAT_ARRAY; for (float v : (float[]) value) { jsonArray.put((double) v); } } else if (value instanceof double[]) { supportedType = TYPE_DOUBLE_ARRAY; for (double v : (double[]) value) { jsonArray.put(v); } } else if (value instanceof boolean[]) { supportedType = TYPE_BOOLEAN_ARRAY; for (boolean v : (boolean[]) value) { jsonArray.put(v); } } else if (value instanceof char[]) { supportedType = TYPE_CHAR_ARRAY; for (char v : (char[]) value) { jsonArray.put(String.valueOf(v)); } } else if (value instanceof List<?>) { supportedType = TYPE_STRING_LIST; @SuppressWarnings("unchecked") List<String> stringList = (List<String>) value; for (String v : stringList) { jsonArray.put((v == null) ? JSONObject.NULL : v); } } else { // Unsupported type. Clear out the array as a precaution even though // it is redundant with the null supportedType. jsonArray = null; } } if (supportedType != null) { json.put(JSON_VALUE_TYPE, supportedType); if (jsonArray != null) { // If we have an array, it has already been converted to JSON. So use // that instead. json.putOpt(JSON_VALUE, jsonArray); } String jsonString = json.toString(); editor.putString(key, jsonString); } }
From source file:reittienEtsinta.tiedostonKasittely.GeoJsonKirjoittaja.java
public static JSONObject munnaJsonPisteet(Reitti kirjoitettava, String crs) { JSONObject reittipisteet = perusJson(crs); JSONArray pistefeatures = new JSONArray(); for (int i = 0; i < kirjoitettava.getAika().length; i++) { double[] reittipiste = new double[] { kirjoitettava.getLon()[i], kirjoitettava.getLat()[i] }; JSONObject pisteenGeometry = new JSONObject(); pisteenGeometry.put("type", "point"); pisteenGeometry.put("coordinates", reittipiste); JSONObject properties = new JSONObject(); properties.put("timefromstart", kirjoitettava.getAika()[i]); if (i == kirjoitettava.getAika().length - 1) { properties.put("length", 0); properties.put("time", 0); properties.put("speed", 0); } else {/* w w w . ja v a 2 s .c om*/ properties.put("length", kirjoitettava.matka(i, i + 1)); properties.put("time", kirjoitettava.aika(i, i + 1)); if (kirjoitettava.aika(i, i + 1) == 0) { properties.put("speed", -1); } else { double vauhti = (double) kirjoitettava.matka(i, i + 1) / (double) kirjoitettava.aika(i, i + 1); properties.put("speed", vauhti); } } properties.put("lat", kirjoitettava.getLat()[i]); properties.put("lon", kirjoitettava.getLon()[i]); JSONObject pointFeature = new JSONObject(); pointFeature.put("properties", properties); pointFeature.put("geometry", pisteenGeometry); pointFeature.put("type", "Feature"); pistefeatures.put(pointFeature); } reittipisteet.put("features", pistefeatures); return reittipisteet; }
From source file:reittienEtsinta.tiedostonKasittely.GeoJsonKirjoittaja.java
public static JSONObject muunnaJsonReitti(Reitti kirjoitettava, String crs) { JSONObject reitti = perusJson(crs);/*from ww w.jav a 2 s. c om*/ JSONArray features = new JSONArray(); JSONObject feature = new JSONObject(); feature.put("type", "Feature"); JSONObject properties = new JSONObject(); feature.put("properties", properties); JSONObject geometry = new JSONObject(); JSONArray coordinates = new JSONArray(); for (int i = 0; i < kirjoitettava.getAika().length; i++) { double[] reittipiste = new double[] { kirjoitettava.getLon()[i], kirjoitettava.getLat()[i] }; coordinates.put(new JSONArray(reittipiste)); } geometry.put("coordinates", coordinates); geometry.put("type", "LineString"); feature.put("geometry", geometry); features.put(feature); reitti.put("features", features); return reitti; }
From source file:org.loklak.api.server.ImportProfileServlet.java
private void doSearch(RemoteAccess.Post post, HttpServletResponse response) throws IOException { String callback = post.get("callback", ""); boolean minified = post.get("minified", false); boolean jsonp = callback != null && callback.length() > 0; String source_type = post.get("source_type", ""); String screen_name = post.get("screen_name", ""); String msg_id = post.get("msg_id", ""); String detailed = post.get("detailed", ""); // source_type either has to be null a a valid SourceType value if (!"".equals(source_type) && !SourceType.hasValue(source_type)) { response.sendError(400, "your request must contain a valid source_type parameter."); return;// ww w . java2 s . c o m } Map<String, String> searchConstraints = new HashMap<>(); if (!"".equals(source_type)) { searchConstraints.put("source_type", source_type); } if (!"".equals(screen_name)) { searchConstraints.put("sharers", screen_name); } if (!"".equals(msg_id)) { searchConstraints.put("imported", msg_id); } Collection<ImportProfileEntry> entries = DAO.SearchLocalImportProfilesWithConstraints(searchConstraints, true); JSONArray entries_to_map = new JSONArray(); for (ImportProfileEntry entry : entries) { JSONObject entry_to_map = entry.toJSON(); if ("true".equals(detailed)) { String query = ""; for (String msgId : entry.getImported()) { query += "id:" + msgId + " "; } DAO.SearchLocalMessages search = new DAO.SearchLocalMessages(query, Timeline.Order.CREATED_AT, 0, 1000, 0); entry_to_map.put("imported", search.timeline.toJSON(false).get("statuses")); } entries_to_map.put(entry_to_map); } post.setResponse(response, "application/javascript"); JSONObject m = new JSONObject(true); JSONObject metadata = new JSONObject(); metadata.put("count", entries.size()); metadata.put("client", post.getClientHost()); m.put("search_metadata", metadata); m.put("profiles", entries_to_map); // write json response.setCharacterEncoding("UTF-8"); PrintWriter sos = response.getWriter(); if (jsonp) sos.print(callback + "("); sos.print(minified ? m.toString() : m.toString(2)); if (jsonp) sos.println(");"); sos.println(); }