List of usage examples for org.json JSONArray length
public int length()
From source file:com.streaming.sweetplayer.fragment.ArtistFragment.java
private void getSongsList(ArrayList<HashMap<String, String>> parseList, String url) { JSONParser jsonParser = new JSONParser(); JSONArray jsonArray; try {/*from ww w. j a va2 s . co m*/ JSONObject json = jsonParser.getJSONFromUrl(url); if (json != null) { jsonArray = json.getJSONArray(mJsonItem); int array_length = jsonArray.length(); if (isSongsView) { for (int i = 0; i < array_length; i++) { HashMap<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = jsonArray.getJSONObject(i); map.put(Config.ID, jsonObject.getString(Config.ID)); map.put(Config.ARTIST, jsonObject.getString(Config.ARTIST)); map.put(Config.NAME, jsonObject.getString(Config.SONG)); map.put(Config.MP3, jsonObject.getString(Config.MP3)); map.put(Config.DURATION, jsonObject.getString(Config.DURATION)); map.put(Config.URL, jsonObject.getString(Config.URL)); map.put(Config.IMAGE, mImageForDB); parseList.add(map); } } else { for (int i = 0; i < array_length; i++) { HashMap<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = jsonArray.getJSONObject(i); map.put(Config.ID, jsonObject.getString(Config.ID)); map.put(Config.ALBUM, jsonObject.getString(Config.ALBUM)); map.put(Config.SONGS_ITEM, jsonObject.getString(Config.SONGS_ITEM)); map.put(Config.IMAGE, jsonObject.getString(Config.IMAGE)); map.put(Config.URL, jsonObject.getString(Config.URL)); parseList.add(map); } } } } catch (JSONException e) { e.printStackTrace(); } }
From source file:uk.thetasinner.concordion.extension.json.internal.JsonComparer.java
private void compareJsonArrays(JSONArray expected, JSONArray actual, String keyPath) { int expectedLength = expected.length(); int actualLength = actual.length(); checkArrayLengthsMatch(expectedLength, actualLength, keyPath); int maxArrayLength = Math.max(expectedLength, actualLength); for (int i = 0; i < maxArrayLength; i++) { if (i >= expectedLength) { jsonDiffLines.add("Extra array element <" + actual.get(i) + ">" + JsonKeyPath.format(keyPath)); continue; }/*w w w. jav a 2 s. c o m*/ if (i >= actualLength) { jsonDiffLines.add("Missing array element <" + expected.get(i) + ">" + JsonKeyPath.format(keyPath)); continue; } routeComparison(expected, actual, keyPath, i); } }
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); }//from ww w . ja va2 s. co m } return new MeasurementLocation(country, state, locality, postalCode); } } } } return null; }
From source file:com.quantcast.measurement.service.QCLocation.java
private boolean containsString(JSONArray arr, String s) { String tmp;//from w ww . j a v a 2 s . c o m for (int i = 0; i < arr.length(); i++) { tmp = arr.optString(i); if (tmp != null && tmp.equals(s)) return true; } return false; }
From source file:tango.util.ProcessingChainsToText.java
public String getPreFilters() { String res = ""; if (json == null) { return null; }// w w w . j a v a 2s . c om 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; }/*w w w . j a va 2 s.c om*/ 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 ww.j av a 2 s . c om*/ * @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 www .jav a 2 s . c o m*/ * @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 {// w ww.j av a 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 w ww . j a v a 2s . c om // 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()); } }