List of usage examples for org.json JSONArray JSONArray
public JSONArray(Object array) throws JSONException
From source file:de.kp.ames.web.function.domain.model.ServiceObject.java
/** * Create ServiceObject from JSON representation * /*w w w.j a v a 2s. c o m*/ * @param jForm * @return * @throws Exception */ public RegistryObjectImpl create(JSONObject jForm) throws Exception { /* * Create SpecificationLink instances */ JSONArray jSpecifications = new JSONArray(jForm.getString(RIM_SPEC)); ArrayList<SpecificationLinkImpl> specificationLinks = new ArrayList<SpecificationLinkImpl>(); for (int i = 0; i < jSpecifications.length(); i++) { JSONObject jSpecification = jSpecifications.getJSONObject(i); String uid = jSpecification.getString(RIM_ID); String seqNo = jSpecification.getString(RIM_SEQNO); RegistryObjectImpl ro = jaxrLCM.getRegistryObjectById(uid); if (ro == null) throw new Exception("[ServiceObject] RegistryObject with id <" + uid + "> does not exist."); /* * Create a new specification link */ SpecificationLinkImpl specificationLink = jaxrLCM.createSpecificationLink(); if (specificationLink == null) throw new Exception("[ServiceObject] Creation of SpecificationLink failed."); SlotImpl slot = jaxrLCM.createSlot(JaxrConstants.SLOT_SEQNO, seqNo, JaxrConstants.SLOT_TYPE); specificationLink.addSlot(slot); specificationLink.setSpecificationObject(ro); specificationLinks.add(specificationLink); } /* * Create ServiceBinding instance */ ServiceBindingImpl serviceBinding = null; if (specificationLinks.isEmpty() == false) { serviceBinding = jaxrLCM.createServiceBinding(); serviceBinding.addSpecificationLinks(specificationLinks); } /* * Create Service instance */ ServiceImpl service = jaxrLCM.createService(jForm.getString(RIM_NAME)); if (jForm.has(RIM_DESC)) service.setDescription(jaxrLCM.createInternationalString(jForm.getString(RIM_DESC))); service.setHome(jaxrHandle.getEndpoint().replace("/saml", "")); if (serviceBinding != null) service.addServiceBinding(serviceBinding); /* * Create classifications */ JSONArray jClases = jForm.has(RIM_CLAS) ? new JSONArray(jForm.getString(RIM_CLAS)) : null; if (jClases != null) { List<ClassificationImpl> classifications = createClassifications(jClases); /* * Set composed object */ service.addClassifications(classifications); } /* * Create slots */ JSONObject jSlots = jForm.has(RIM_SLOT) ? new JSONObject(jForm.getString(RIM_SLOT)) : null; if (jSlots != null) { List<SlotImpl> slots = createSlots(jSlots); /* * Set composed object */ service.addSlots(slots); } /* * Indicate as created */ this.created = true; return service; }
From source file:de.kp.ames.web.function.domain.model.ServiceObject.java
/** * Update ServiceObject from JSON representation * // w w w . j a v a2 s .c om * @param jForm * @return * @throws Exception */ public RegistryObjectImpl update(JSONObject jForm) throws Exception { /* * Determine service from unique identifier */ String sid = jForm.getString(RIM_ID); ServiceImpl service = (ServiceImpl) jaxrLCM.getRegistryObjectById(sid); if (service == null) throw new Exception("[ServiceObject] RegistryObject with id <" + sid + "> does not exist."); /* * Remove service binding and associated specification links */ Collection<?> bindings = service.getServiceBindings(); if (bindings.isEmpty() == false) service.removeServiceBindings(bindings); /* * SpecificationLink instances */ JSONArray jSpecifications = new JSONArray(jForm.getString(RIM_SPEC)); ArrayList<SpecificationLinkImpl> specificationLinks = new ArrayList<SpecificationLinkImpl>(); for (int i = 0; i < jSpecifications.length(); i++) { JSONObject jSpecification = jSpecifications.getJSONObject(i); String uid = jSpecification.getString(RIM_ID); String seqNo = jSpecification.getString(RIM_SEQNO); RegistryObjectImpl ro = jaxrLCM.getRegistryObjectById(uid); if (ro == null) throw new Exception("[ServiceObject] RegistryObject with id <" + uid + "> does not exist."); /* * Create a new specification link */ SpecificationLinkImpl specificationLink = jaxrLCM.createSpecificationLink(); if (specificationLink == null) throw new Exception("[ServiceObject] Creation of SpecificationLink failed."); SlotImpl slot = jaxrLCM.createSlot(JaxrConstants.SLOT_SEQNO, seqNo, JaxrConstants.SLOT_TYPE); specificationLink.addSlot(slot); specificationLink.setSpecificationObject(ro); specificationLinks.add(specificationLink); } /* * Create ServiceBinding instance */ ServiceBindingImpl serviceBinding = null; if (specificationLinks.isEmpty() == false) { serviceBinding = jaxrLCM.createServiceBinding(); serviceBinding.addSpecificationLinks(specificationLinks); } if (serviceBinding != null) service.addServiceBinding(serviceBinding); /* * Update core metadata */ updateMetadata(service, jForm); // /* // * Name & description // */ // if (jForm.has(RIM_NAME)) service.setName(jaxrLCM.createInternationalString(jForm.getString(RIM_NAME))); // if (jForm.has(RIM_DESC)) service.setDescription(jaxrLCM.createInternationalString(jForm.getString(RIM_DESC))); // // /* // * Update slots // */ // JSONObject jSlots = jForm.has(RIM_SLOT) ? new JSONObject(jForm.getString(RIM_SLOT)) : null; // if (jSlots != null) { // // List<SlotImpl> slots = updateSlots(service, jSlots); // /* // * Set composed object // */ // service.addSlots(slots); // // } /* * Indicate as updated */ this.created = false; return service; }
From source file:com.intel.xdk.camera.Camera.java
/** * Executes the request and returns PluginResult. *//from w w w . jav a 2 s .c o m * @param action The action to execute. * @param args JSONArray of arguments for the plugin. * @param callbackContext The callback context used when calling back into JavaScript. * @return True when the action was valid, false otherwise. */ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if (action.equals("clearPictures")) { this.clearPictures(); } else if (action.equals("deletePicture")) { this.deletePicture(args.getString(0)); } else if (action.equals("getInfo")) { //get pictureLocation and pictureList JSONObject r = new JSONObject(); r.put("pictureLocation", pictureDir()); List<String> pictureList = getPictureList(); r.put("pictureList", new JSONArray(pictureList)); callbackContext.success(r); } else if (action.equals("importPicture")) { this.importPicture(); this.callbackContext = callbackContext; } else if (action.equals("takePicture")) { this.takePicture(args.getInt(0), args.getString(1), args.getString(2)); this.callbackContext = callbackContext; } else { return false; } // All actions are async. //callbackContext.success(); return true; }
From source file:net.idlesoft.android.apps.github.activities.CommitsList.java
@Override public void onRestoreInstanceState(final Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); try {//from www . j ava 2s . c om if (savedInstanceState.containsKey("commitsJson")) { mCommitsJSON = new JSONArray(savedInstanceState.getString("commitsJson")); } } catch (final Exception e) { e.printStackTrace(); return; } if (mCommitsJSON != null) { mCommitListAdapter = new CommitListAdapter(this, mCommitsJSON); } }
From source file:damo.three.ie.prepay.InternetAddonAlarmManagerReceiver.java
@Override public void onReceive(Context context, Intent intent) { SharedPreferences sharedPrefsUsages = context.getSharedPreferences("damo.three.ie.previous_usage", Context.MODE_PRIVATE); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); boolean notificationsEnabled = sharedPrefs.getBoolean("notification", true); String usages = sharedPrefsUsages.getString("usage_info", null); // first check if anything was persisted if (usages != null) { try {// ww w .j a v a 2 s . c om List<UsageItem> usageItems = JSONUtils.jsonToUsageItems(new JSONArray(usages)); List<BasicUsageItem> basicUsageItems = UsageUtils.getAllBasicItems(usageItems); UsageUtils.registerInternetExpireAlarm(context, basicUsageItems, notificationsEnabled, false); } catch (JSONException e) { e.printStackTrace(); } } }
From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java
private void setCanvasAccoutId() throws IOException, JSONException { String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/course_accounts"; RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap); JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output")); String accountId = apiResponseArray.getJSONObject(0).getString("id"); connectorProperties.put("accountId", accountId); }
From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java
/** * Test listCourses method with Mandatory Parameters. *//*ww w . j ava 2s . c om*/ @Test(groups = { "wso2.esb" }, dependsOnMethods = { "testCreateCourseWithMandatoryParameters", "testCreateCourseWithOptionalParameters" }, description = "canvas {listCourses} integration test with mandatory parameters.") public void testListCoursesWithMandatoryParameters() throws IOException, JSONException { esbRequestHeadersMap.put("Action", "urn:listCourses"); RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_listCourses_mandatory.json"); JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output")); String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses"; RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap); JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output")); Assert.assertEquals(esbResponseArray.length(), apiResponseArray.length()); Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("id"), apiResponseArray.getJSONObject(0).getString("id")); Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("name"), apiResponseArray.getJSONObject(0).getString("name")); Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("course_code"), apiResponseArray.getJSONObject(0).getString("course_code")); Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("start_at"), apiResponseArray.getJSONObject(0).getString("start_at")); Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("end_at"), apiResponseArray.getJSONObject(0).getString("end_at")); }
From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java
/** * Test listCourses method with Optional Parameters. */// w w w . j a v a2 s.c o m @Test(groups = { "wso2.esb" }, dependsOnMethods = { "testCreateCourseWithMandatoryParameters", "testCreateCourseWithOptionalParameters" }, description = "canvas {listCourses} integration test with optional parameters.") public void testListCoursesWithOptionalParameters() throws IOException, JSONException { esbRequestHeadersMap.put("Action", "urn:listCourses"); RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_listCourses_optional.json"); JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output")); String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses" + "?include[]=term&include[]=sections"; RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap); JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output")); Assert.assertEquals(esbResponseArray.length(), apiResponseArray.length()); // Term details Assert.assertEquals(esbResponseArray.getJSONObject(0).getJSONObject("term").getString("id"), apiResponseArray.getJSONObject(0).getJSONObject("term").getString("id")); Assert.assertEquals(esbResponseArray.getJSONObject(0).getJSONObject("term").getString("name"), apiResponseArray.getJSONObject(0).getJSONObject("term").getString("name")); // Section details Assert.assertEquals( esbResponseArray.getJSONObject(0).getJSONArray("sections").getJSONObject(0).getString("id"), apiResponseArray.getJSONObject(0).getJSONArray("sections").getJSONObject(0).getString("id")); Assert.assertEquals( esbResponseArray.getJSONObject(0).getJSONArray("sections").getJSONObject(0).getString("name"), apiResponseArray.getJSONObject(0).getJSONArray("sections").getJSONObject(0).getString("name")); }
From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java
/** * Test listCourseUsers method with Mandatory Parameters. *///from w ww . j a va 2 s . c o m @Test(groups = { "wso2.esb" }, dependsOnMethods = { "testCreateCourseWithMandatoryParameters" }, description = "canvas {listCourseUsers} integration test with mandatory parameters.") public void testListCourseUsersWithMandatoryParameters() throws IOException, JSONException { esbRequestHeadersMap.put("Action", "urn:listCourseUsers"); RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_listCourseUsers_mandatory.json"); JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output")); connectorProperties.put("userId", esbResponseArray.getJSONObject(0).getString("id")); String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses/" + connectorProperties.getProperty("courseId") + "/users"; RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap); JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output")); Assert.assertEquals(esbResponseArray.length(), apiResponseArray.length()); Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("id"), apiResponseArray.getJSONObject(0).getString("id")); Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("name"), apiResponseArray.getJSONObject(0).getString("name")); Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("sortable_name"), apiResponseArray.getJSONObject(0).getString("sortable_name")); Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("login_id"), apiResponseArray.getJSONObject(0).getString("login_id")); }
From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java
/** * Test listCourseUsers method with Optional Parameters. *//*from w w w .j a v a2s .co m*/ @Test(groups = { "wso2.esb" }, dependsOnMethods = { "testCreateCourseWithOptionalParameters" }, description = "canvas {listCourseUsers} integration test with optional parameters.") public void testListCourseUsersWithOptionalParameters() throws IOException, JSONException { esbRequestHeadersMap.put("Action", "urn:listCourseUsers"); RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_listCourseUsers_optional.json"); JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output")); String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses/" + connectorProperties.getProperty("courseIdOptional") + "/users" + "?include[]=email&include[]=enrollments"; RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap); JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output")); Assert.assertEquals(esbResponseArray.length(), apiResponseArray.length()); // ID and Email details Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("id"), apiResponseArray.getJSONObject(0).getString("id")); Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("email"), apiResponseArray.getJSONObject(0).getString("email")); // Enrollment details Assert.assertEquals( esbResponseArray.getJSONObject(0).getJSONArray("enrollments").getJSONObject(0).getString("role"), apiResponseArray.getJSONObject(0).getJSONArray("enrollments").getJSONObject(0).getString("role")); Assert.assertEquals( esbResponseArray.getJSONObject(0).getJSONArray("enrollments").getJSONObject(0) .getString("course_id"), apiResponseArray.getJSONObject(0).getJSONArray("enrollments").getJSONObject(0) .getString("course_id")); }