List of usage examples for org.json JSONArray getJSONObject
public JSONObject getJSONObject(int index) throws JSONException
From source file:com.quantcast.measurement.service.QCLocation.java
private MeasurementLocation parseJson(String jsonString) throws JSONException { JSONObject json = new JSONObject(jsonString); if (OK.equals(json.optString(STATUS))) { JSONArray resultsArray = json.optJSONArray(RESULTS); if (resultsArray != null) { JSONArray addressComponents; for (int i = 0; i < resultsArray.length(); i++) { addressComponents = resultsArray.getJSONObject(i).optJSONArray(ADDRESS); if (addressComponents != null) { String country = "", locality = "", state = "", postalCode = ""; for (int j = 0; j < addressComponents.length(); j++) { JSONObject obj = addressComponents.getJSONObject(j); JSONArray types = obj.optJSONArray(TYPES); if (types != null) { if (containsString(types, LOCALITY)) locality = obj.getString(SHORT_NAME); if (containsString(types, STATE)) state = obj.getString(SHORT_NAME); if (containsString(types, COUNTRY)) country = obj.getString(SHORT_NAME); if (containsString(types, POSTAL_CODE)) postalCode = obj.getString(SHORT_NAME); }/* w w w . ja v a2 s . c om*/ } return new MeasurementLocation(country, state, locality, postalCode); } } } } return null; }
From source file:tango.util.ProcessingChainsToText.java
public String getPreFilters() { String res = ""; if (json == null) { return null; }/*from ww w. ja v a 2 s. c o m*/ JSONArray pre = json.getJSONArray("preFilters"); int nb = pre.length(); //res = res.concat("prefilters " + pre.length()); for (int pf = 0; pf < nb; pf++) { JSONObject pre0 = pre.getJSONObject(pf); res = res.concat("* Prefilter " + (pf + 1) + "\n"); res = res.concat(getString(pre0)); } return res; }
From source file:tango.util.ProcessingChainsToText.java
public String getPostFilters() { String res = ""; if (json == null) { return null; }//from www . j a va 2s. co m JSONArray pre = json.getJSONArray("postFilters"); int nb = pre.length(); //res = res.concat("prefilters " + pre.length()); for (int pf = 0; pf < nb; pf++) { JSONObject pre0 = pre.getJSONObject(pf); res = res.concat("* PostFilter " + (pf + 1) + "\n"); res = res.concat(getString(pre0)); } return res; }
From source file:de.kp.ames.web.function.domain.model.ServiceObject.java
/** * Create ServiceObject from JSON representation * //from w w w. j a v a 2 s .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 * //from ww w .j a va 2s. 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.vk.sdkweb.api.model.VKApiArray.java
public void parse(JSONArray jsonArray) { items = new ArrayList<T>(jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { try {//from w w w. j a va 2 s .co m items.add(parseNextObject(jsonArray.getJSONObject(i))); } catch (JSONException e) { if (VKSdk.DEBUG) e.printStackTrace(); } } if (count == 0) count = items.size(); }
From source file:com.gvccracing.android.tttimer.Tabs.RaceInfoTab.java
public void postData() { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost( "http://www.gvccracing.com/?page_id=2525&pass=com.gvccracing.android.tttimer"); try {//from ww w .j a va 2 s . c o m // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(20); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert the Bytes read to a String. */ String text = new String(baf.toByteArray()); JSONObject mainJson = new JSONObject(text); JSONArray jsonArray = mainJson.getJSONArray("members"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject json = jsonArray.getJSONObject(i); String firstName = json.getString("fname"); String lastName = json.getString("lname"); String licenseStr = json.getString("license"); Integer license = 0; try { license = Integer.parseInt(licenseStr); } catch (Exception ex) { Log.e(LOG_TAG(), "Unable to parse license string"); } long age = json.getLong("age"); String categoryStr = json.getString("category"); Integer category = 5; try { category = Integer.parseInt(categoryStr); } catch (Exception ex) { Log.e(LOG_TAG(), "Unable to parse category string"); } String phone = json.getString("phone"); long phoneNumber = 0; try { phoneNumber = Long.parseLong(phone.replace("-", "").replace("(", "").replace(")", "") .replace(" ", "").replace(".", "").replace("*", "")); } catch (Exception e) { Log.e(LOG_TAG(), "Unable to parse phone number"); } String gender = json.getString("gender"); String econtact = json.getString("econtact"); String econtactPhone = json.getString("econtact_phone"); long eContactPhoneNumber = 0; try { eContactPhoneNumber = Long.parseLong(econtactPhone.replace("-", "").replace("(", "") .replace(")", "").replace(" ", "").replace(".", "").replace("*", "")); } catch (Exception e) { Log.e(LOG_TAG(), "Unable to parse econtact phone number"); } Long member_id = json.getLong("member_id"); String gvccCategory; switch (category) { case 1: case 2: case 3: gvccCategory = "A"; break; case 4: gvccCategory = "B4"; break; case 5: gvccCategory = "B5"; break; default: gvccCategory = "B5"; break; } Log.w(LOG_TAG(), lastName); Cursor racerInfo = Racer.Instance().Read(getActivity(), new String[] { Racer._ID, Racer.FirstName, Racer.LastName, Racer.USACNumber, Racer.PhoneNumber, Racer.EmergencyContactName, Racer.EmergencyContactPhoneNumber }, Racer.USACNumber + "=?", new String[] { license.toString() }, null); if (racerInfo.getCount() > 0) { racerInfo.moveToFirst(); Long racerID = racerInfo.getLong(racerInfo.getColumnIndex(Racer._ID)); Racer.Instance().Update(getActivity(), racerID, firstName, lastName, license, 0l, phoneNumber, econtact, eContactPhoneNumber, gender); Cursor racerClubInfo = RacerClubInfo.Instance().Read(getActivity(), new String[] { RacerClubInfo._ID, RacerClubInfo.GVCCID, RacerClubInfo.RacerAge, RacerClubInfo.Category }, RacerClubInfo.Racer_ID + "=? AND " + RacerClubInfo.Year + "=? AND " + RacerClubInfo.Upgraded + "=?", new String[] { racerID.toString(), "2013", "0" }, null); if (racerClubInfo.getCount() > 0) { racerClubInfo.moveToFirst(); long racerClubInfoID = racerClubInfo .getLong(racerClubInfo.getColumnIndex(RacerClubInfo._ID)); String rciCategory = racerClubInfo .getString(racerClubInfo.getColumnIndex(RacerClubInfo.Category)); boolean upgraded = gvccCategory != rciCategory; if (upgraded) { RacerClubInfo.Instance().Update(getActivity(), racerClubInfoID, null, null, null, null, null, null, null, null, null, upgraded); RacerClubInfo.Instance().Create(getActivity(), racerID, null, 2013, gvccCategory, 0, 0, 0, age, member_id, false); } else { RacerClubInfo.Instance().Update(getActivity(), racerClubInfoID, null, null, null, null, null, null, null, age, member_id, upgraded); } } else { RacerClubInfo.Instance().Create(getActivity(), racerID, null, 2013, gvccCategory, 0, 0, 0, age, member_id, false); } if (racerClubInfo != null) { racerClubInfo.close(); } } else { // TODO: Better birth date Uri resultUri = Racer.Instance().Create(getActivity(), firstName, lastName, license, 0l, phoneNumber, econtact, eContactPhoneNumber, gender); long racerID = Long.parseLong(resultUri.getLastPathSegment()); RacerClubInfo.Instance().Create(getActivity(), racerID, null, 2013, gvccCategory, 0, 0, 0, age, member_id, false); } if (racerInfo != null) { racerInfo.close(); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { Log.e(LOG_TAG(), e.getMessage()); } }
From source file:com.namelessdev.mpdroid.cover.ItunesCover.java
@Override public String[] getCoverUrl(final AlbumInfo albumInfo) throws Exception { final String response; final JSONObject jsonRootObject; final JSONArray jsonArray; String coverUrl;/* ww w . j a va 2s. co m*/ JSONObject jsonObject; try { response = executeGetRequest("https://itunes.apple.com/search?term=" + albumInfo.getAlbum() + ' ' + albumInfo.getArtist() + "&limit=5&media=music&entity=album"); jsonRootObject = new JSONObject(response); jsonArray = jsonRootObject.getJSONArray("results"); for (int i = 0; i < jsonArray.length(); i++) { jsonObject = jsonArray.getJSONObject(i); coverUrl = jsonObject.getString("artworkUrl100"); if (coverUrl != null) { // Based on some tests even if the cover art size returned // is 100x100 // Bigger versions also exists. return new String[] { coverUrl.replace("100x100", "600x600") }; } } } catch (final Exception e) { if (CoverManager.DEBUG) { Log.e(TAG, "Failed to get cover URL from " + getName(), e); } } return new String[0]; }
From source file:mr.robotto.engine.loader.components.shader.MrShaderProgramLoader.java
private void loadUniforms(MrShaderProgram program) throws JSONException { JSONArray uniformsJsonArray = mRoot.getJSONArray("Uniforms"); for (int i = 0; i < uniformsJsonArray.length(); i++) { JSONObject uniformJson = uniformsJsonArray.getJSONObject(i); MrUniformLoader uniformLoader = new MrUniformLoader(uniformJson); MrUniform uniform = uniformLoader.parse(); program.addUniform(uniform);/*from ww w . j a v a 2 s . com*/ } }
From source file:mr.robotto.engine.loader.components.shader.MrShaderProgramLoader.java
private void loadAttributes(MrShaderProgram program) throws JSONException { JSONArray attributesJsonArray = mRoot.getJSONArray("Attributes"); for (int i = 0; i < attributesJsonArray.length(); i++) { JSONObject attributeJson = attributesJsonArray.getJSONObject(i); MrAttributeLoader attributeLoader = new MrAttributeLoader(attributeJson); MrAttribute attribute = attributeLoader.parse(); program.addAttribute(attribute); }// www .ja va2 s .c o m }