List of usage examples for org.json JSONObject length
public int length()
From source file:org.loklak.server.AbstractAPIHandler.java
@Override public JSONObject[] service(Query call, Authorization rights) throws APIException { // make call to the embedded api if (this.serverProtocolHostStub == null) return new JSONObject[] { serviceImpl(call, null, rights, rights.getPermissions(this)) }; // make call(s) to a remote api(s) JSONObject[] results = new JSONObject[this.serverProtocolHostStub.length]; for (int rc = 0; rc < results.length; rc++) { try {/*from w ww . j a v a2 s . co m*/ StringBuilder urlquery = new StringBuilder(); for (String key : call.getKeys()) { urlquery.append(urlquery.length() == 0 ? '?' : '&').append(key).append('=') .append(call.get(key, "")); } String urlstring = this.serverProtocolHostStub[rc] + this.getAPIPath() + urlquery.toString(); byte[] jsonb = ClientConnection.download(urlstring); if (jsonb == null || jsonb.length == 0) throw new IOException("empty content from " + urlstring); String jsons = UTF8.String(jsonb); JSONObject json = new JSONObject(jsons); if (json == null || json.length() == 0) { results[rc] = null; continue; } ; results[rc] = json; } catch (Throwable e) { Log.getLog().warn(e); } } return results; }
From source file:com.cmackay.plugins.googleanalytics.GoogleAnalyticsPlugin.java
private static Map<String, String> objectToMap(JSONObject o) throws JSONException { if (o.length() == 0) { return Collections.<String, String>emptyMap(); }// www.j av a 2 s .c o m Map<String, String> map = new HashMap<String, String>(o.length()); Iterator it = o.keys(); String key, value; while (it.hasNext()) { key = it.next().toString(); value = o.has(key) ? o.get(key).toString() : null; map.put(key, value); } return map; }
From source file:org.collectionspace.chain.csp.persistence.services.relation.TestRelationsThroughWebapp.java
@Test public void testRelationsCreate() throws Exception { // First create atester. couple of 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 a v a2s. co m 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("/"); // Now create a pair of relations in: 3<->1, 3->2: a. 1->3, b. 3->1, c. // 3->2 out = tester.POSTData("/relationships", createRelation(path3[1], path3[2], "affects", path1[1], path1[2], false), jetty); log.info(out.getContent()); String relid1 = out.getHeader("Location"); String csid1 = new JSONObject(out.getContent()).getString("csid"); out = tester.POSTData("/relationships", createRelation(path3[1], path3[2], "affects", path2[1], path2[2], true), jetty); log.info(out.getContent()); String relid2 = out.getHeader("Location"); String csid2 = new JSONObject(out.getContent()).getString("csid"); // Check 1 has relation to 3 out = tester.GETData(id1, jetty); JSONObject data1 = new JSONObject(out.getContent()); // that the destination is 3 log.info(out.getContent()); JSONArray rel1 = data1.getJSONObject("relations").getJSONArray("cataloging"); assertNotNull(rel1); assertEquals(1, rel1.length()); JSONObject mini1 = rel1.getJSONObject(0); assertEquals("cataloging", mini1.getString("recordtype")); assertEquals(mini1.getString("csid"), path3[2]); /* * relid and relationshiptype are no longer provided in the relation payload String rida = mini1.getString("relid"); // pull the relation itself, and check it out = tester.GETData("/relationships/" + rida, jetty); JSONObject rd1 = new JSONObject(out.getContent()); assertEquals("affects", rd1.getString("type")); assertEquals(rida, rd1.getString("csid")); JSONObject src1 = rd1.getJSONObject("source"); assertEquals("cataloging", src1.getString("recordtype")); assertEquals(path1[2], src1.get("csid")); JSONObject dst1 = rd1.getJSONObject("target"); assertEquals("cataloging", dst1.getString("recordtype")); assertEquals(path3[2], dst1.get("csid")); */ // Check that 2 has no relations at all out = tester.GETData(id2, jetty); JSONObject data2 = new JSONObject(out.getContent()); // that the destination is 3 JSONObject rel2 = data2.getJSONObject("relations"); assertNotNull(rel2); assertEquals(0, rel2.length()); // Check that 3 has relations to 1 and 2 out = tester.GETData(id3, jetty); JSONObject data3 = new JSONObject(out.getContent()); // untangle them JSONArray rel3 = data3.getJSONObject("relations").getJSONArray("cataloging"); assertNotNull(rel3); assertEquals(2, rel3.length()); int i0 = 0, i1 = 1; String rel_a = rel3.getJSONObject(i0).getString("csid"); String rel_b = rel3.getJSONObject(i1).getString("csid"); if (rel_a.equals(path2[2]) && rel_b.equals(path1[2])) { i0 = 1; i1 = 0; } JSONObject rel31 = rel3.getJSONObject(i0); JSONObject rel32 = rel3.getJSONObject(i1); // check desintations assertEquals("cataloging", rel31.getString("recordtype")); assertEquals(rel31.getString("csid"), path1[2]); /* * relid no longer provided in the payloads */ //String rid31 = rel31.getString("relid"); assertEquals("cataloging", rel32.getString("recordtype")); assertEquals(rel32.getString("csid"), path2[2]); //String rid32 = rel32.getString("relid"); // check actual records // 3 -> 1 // out = tester.GETData("/relationships/" + rid31, jetty); // JSONObject rd31 = new JSONObject(out.getContent()); // assertEquals("affects", rd31.getString("type")); // assertEquals(rid31, rd31.getString("csid")); // JSONObject src31 = rd31.getJSONObject("source"); // assertEquals("cataloging", src31.getString("recordtype")); // assertEquals(path3[2], src31.get("csid")); // JSONObject dst31 = rd31.getJSONObject("target"); // assertEquals("cataloging", dst31.getString("recordtype")); // assertEquals(path1[2], dst31.get("csid")); // 3 -> 2 // out = tester.GETData("/relationships/" + rid32, jetty); // JSONObject rd32 = new JSONObject(out.getContent()); // assertEquals("affects", rd32.getString("type")); // assertEquals(rid32, rd32.getString("csid")); // JSONObject src32 = rd32.getJSONObject("source"); // assertEquals("cataloging", src32.getString("recordtype")); // assertEquals(path3[2], src32.get("csid")); // JSONObject dst32 = rd32.getJSONObject("target"); // assertEquals("cataloging", dst32.getString("recordtype")); // assertEquals(path2[2], dst32.get("csid")); /* clean up */ tester.DELETEData("/relationships/" + csid1, jetty); tester.DELETEData("/relationships/" + csid2, jetty); tester.DELETEData(id1, jetty); tester.DELETEData(id2, jetty); tester.DELETEData(id3, jetty); tester.stopJetty(jetty); }
From source file:org.collectionspace.chain.csp.persistence.services.relation.TestRelationsThroughWebapp.java
@Test public void testUpdate() 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 ww w . j a va 2 s. c o m*/ 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("/"); // Create a relation 3 -> 1 out = tester.POSTData("/relationships", createRelation(path3[1], path3[2], "affects", path1[1], path1[2], true), jetty); // Get csid JSONObject data = new JSONObject(out.getContent()); log.info(out.getContent()); String csid1 = data.getString("csid"); assertNotNull(csid1); // Update it to 2 -> 1 out = tester.PUTData("/relationships/" + csid1, createRelation(path2[1], path2[2], "affects", path1[1], path1[2], true), jetty); log.info(out.getContent()); // Check it out = tester.GETData(id1, jetty); JSONObject data1 = new JSONObject(out.getContent()); JSONObject rel1 = data1.getJSONObject("relations"); out = tester.GETData(id2, jetty); JSONObject data2 = new JSONObject(out.getContent()); log.info(out.getContent()); JSONArray rel2 = data2.getJSONObject("relations").getJSONArray("cataloging"); out = tester.GETData(id3, jetty); JSONObject data3 = new JSONObject(out.getContent()); JSONObject rel3 = data3.getJSONObject("relations"); // clean up after tester.DELETEData("/relationships/" + csid1, jetty); tester.DELETEData(id1, jetty); tester.DELETEData(id2, jetty); tester.DELETEData(id3, jetty); //test assertNotNull(rel1); assertEquals(0, rel1.length()); assertNotNull(rel2); assertEquals(1, rel2.length()); assertNotNull(rel3); assertEquals(0, rel3.length()); tester.stopJetty(jetty); }
From source file:org.collectionspace.chain.csp.persistence.services.relation.TestRelationsThroughWebapp.java
@Test public void testOneWayWorksInUpdate() throws Exception { ServletTester jetty = tester.setupJetty(); HttpTester out = tester.POSTData("/intake/", tester.makeSimpleRequest(tester.getResourceString("2007.4-a.json")), jetty); String id1 = out.getHeader("Location"); out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty);/*from w w w .ja v a 2 s. c om*/ String id2 = out.getHeader("Location"); out = tester.POSTData("/acquisition/", tester.makeSimpleRequest(tester.getResourceString("2005.017.json")), jetty); String id3 = out.getHeader("Location"); String[] path1 = id1.split("/"); String[] path2 = id2.split("/"); String[] path3 = id3.split("/"); // Create a relation 3 <-> 1 out = tester.POSTData("/relationships", createRelation(path3[1], path3[2], "affects", path1[1], path1[2], false), jetty); // Get csid JSONObject data = new JSONObject(out.getContent()); String csid1 = data.getString("csid"); assertNotNull(csid1); // Update to 2 <-> 1 keeping one-way false out = tester.PUTData("/relationships/" + csid1, createRelation(path2[1], path2[2], "affects", path1[1], path1[2], false), jetty); // Check it out = tester.GETData(id1, jetty); JSONObject data1 = new JSONObject(out.getContent()); JSONArray rel1 = data1.getJSONObject("relations").getJSONArray("cataloging"); assertNotNull(rel1); assertEquals(1, rel1.length()); out = tester.GETData(id2, jetty); JSONObject data2 = new JSONObject(out.getContent()); JSONArray rel2 = data2.getJSONObject("relations").getJSONArray("intake"); assertNotNull(rel2); assertEquals(1, rel2.length()); out = tester.GETData(id3, jetty); JSONObject data3 = new JSONObject(out.getContent()); JSONObject rel3 = data3.getJSONObject("relations"); assertNotNull(rel3); assertEquals(0, rel3.length()); // Update to 1 -> 3, making one-way true //String csid2 = rel1.getJSONObject(0).getString("relid"); out = tester.PUTData("/relationships/" + csid1, createRelation(path1[1], path1[2], "affects", path3[1], path3[2], true), jetty); // Check it out = tester.GETData(id1, jetty); data1 = new JSONObject(out.getContent()); rel1 = data1.getJSONObject("relations").getJSONArray("acquisition"); assertNotNull(rel1); assertEquals(1, rel1.length()); out = tester.GETData(id2, jetty); data2 = new JSONObject(out.getContent()); JSONObject rel2a = data2.getJSONObject("relations"); assertNotNull(rel2a); assertEquals(0, rel2a.length()); out = tester.GETData(id3, jetty); data3 = new JSONObject(out.getContent()); rel3 = data3.getJSONObject("relations"); assertNotNull(rel3); assertEquals(0, rel3.length()); // Update to 3 -> 1, keeping one way true out = tester.PUTData("/relationships/" + csid1, createRelation(path3[1], path3[2], "affects", path1[1], path1[2], true), jetty); // Check it out = tester.GETData(id1, jetty); data1 = new JSONObject(out.getContent()); JSONObject rel1a = data1.getJSONObject("relations"); assertNotNull(rel1a); assertEquals(0, rel1a.length()); out = tester.GETData(id2, jetty); data2 = new JSONObject(out.getContent()); rel2a = data2.getJSONObject("relations"); assertNotNull(rel2a); assertEquals(0, rel2a.length()); out = tester.GETData(id3, jetty); data3 = new JSONObject(out.getContent()); JSONArray rel3a = data3.getJSONObject("relations").getJSONArray("intake"); assertNotNull(rel3a); assertEquals(1, rel3a.length()); // Update to 1 <-> 2, making one way false out = tester.PUTData("/relationships/" + csid1, createRelation(path1[1], path1[2], "affects", path2[1], path2[2], false), jetty); // Check it out = tester.GETData(id1, jetty); data1 = new JSONObject(out.getContent()); rel1 = data1.getJSONObject("relations").getJSONArray("cataloging"); assertNotNull(rel1); assertEquals(1, rel1.length()); out = tester.GETData(id2, jetty); data2 = new JSONObject(out.getContent()); rel2 = data2.getJSONObject("relations").getJSONArray("intake"); assertNotNull(rel2); assertEquals(1, rel2.length()); out = tester.GETData(id3, jetty); data3 = new JSONObject(out.getContent()); rel3 = data3.getJSONObject("relations"); assertNotNull(rel3); assertEquals(0, rel3.length()); // clean up after tester.DELETEData(id1, jetty); tester.DELETEData(id2, jetty); tester.DELETEData(id3, jetty); tester.stopJetty(jetty); }
From source file:com.snappy.couchdb.CouchDBHelper.java
/** * Parse a single user JSON object/*from www . j a va 2 s.c om*/ * * @param json * @return * @throws JSONException * @throws SpikaException */ public static User parseSingleUserObjectWithoutRowParam(JSONObject userJson) throws JSONException, SpikaException { User user = null; ArrayList<String> contactsIds = new ArrayList<String>(); if (userJson != null) { if (userJson.length() == 0) { return null; } if (userJson.has(Const.ERROR)) { appLogout(null, false, isInvalidToken(userJson)); throw new SpikaException(ConnectionHandler.getError(userJson)); } user = sGsonExpose.fromJson(userJson.toString(), User.class); if (userJson.has(Const.FAVORITE_GROUPS)) { JSONArray favorite_groups = userJson.getJSONArray(Const.FAVORITE_GROUPS); List<String> groups = new ArrayList<String>(); for (int i = 0; i < favorite_groups.length(); i++) { groups.add(favorite_groups.getString(i)); } user.setGroupIds(groups); } if (userJson.has(Const.CONTACTS)) { JSONArray contacts = userJson.getJSONArray(Const.CONTACTS); for (int i = 0; i < contacts.length(); i++) { contactsIds.add(contacts.getString(i)); } user.setContactIds(contactsIds); } } return user; }
From source file:com.snappy.couchdb.CouchDBHelper.java
/** * Parse single JSON object of type Group * * @param json/* ww w . ja va 2 s . c om*/ * @return */ public static Group parseSingleGroupObjectWithoutRowParam(JSONObject json) { Group group = null; if (json != null) { if (json.has(Const.ERROR)) { appLogout(null, false, isInvalidToken(json)); return null; } if (json.length() == 0) { return null; } if (json.has(Const.NAME)) { group = sGsonExpose.fromJson(json.toString(), Group.class); } } return group; }
From source file:com.geekandroid.sdk.sample.JPushReceiver.java
private void processCustomMessage(Context context, Bundle bundle) { // if (MainActivity.isForeground) { String message = bundle.getString(JPushInterface.EXTRA_MESSAGE); String extras = bundle.getString(JPushInterface.EXTRA_EXTRA); Intent msgIntent = new Intent(MESSAGE_RECEIVED_ACTION); msgIntent.putExtra(KEY_MESSAGE, message); if (!isEmpty(extras)) { try {//from w ww .ja v a 2 s . c om JSONObject extraJson = new JSONObject(extras); if (null != extraJson && extraJson.length() > 0) { msgIntent.putExtra(KEY_EXTRAS, extras); } } catch (JSONException e) { } } context.sendBroadcast(msgIntent); // } }
From source file:damian.hashmap.MyActivity.java
public void generateLicenses() throws JSONException { JSONArray obj = new JSONArray(loadJSONFromAsset()); for (int i = 0; i < obj.length(); i++) { JSONObject obj2 = obj.getJSONObject(i); String name = obj2.getString("name"); int dni = obj2.getInt("dni"); JSONObject license = obj2.getJSONObject("license"); License license1 = new License(); if (license.length() > 0) { license1.setName(name);/* w w w . j av a 2 s.co m*/ long since = license.getLong("since"); long until = license.getLong("until"); license1.setSince(since); license1.setUntil(until); } Person person = new Person(); person.setDni(dni); person.setName(name); if (license.length() > 0) { licences.put(person, license1); } else { licences.put(person, null); } } }
From source file:org.envirocar.app.json.TrackEncoder.java
private JSONObject createMeasurementProperties(Measurement measurement, String trackSensorName) throws JSONException { JSONObject result = new JSONObject(); result.put("sensor", trackSensorName); JSONObject phens = createPhenomenons(measurement); if (phens != null && phens.length() > 0) { result.put("phenomenons", phens); }// ww w . jav a 2 s .c o m result.put("time", Util.longToIsoDate(measurement.getTime())); return result; }