List of usage examples for org.json JSONObject getJSONArray
public JSONArray getJSONArray(String key) throws JSONException
From source file:de.devmil.common.licensing.LicenseInfo.java
public static LicenseInfo readFromJSON(JSONObject obj) { LicenseInfo result = new LicenseInfo(); JSONArray licenses = null;//from w ww .j a va2s .c om try { licenses = obj.getJSONArray(LICENSE_ARRAY_IDENTIFIER); } catch (JSONException e) { LOGW(TAG, "Error reading LicenseInfo", e); return null; } for (int i = 0; i < licenses.length(); i++) { try { JSONObject licenseObj = licenses.getJSONObject(i); LicenseDefinition ld = LicenseDefinition.readFromJSON(licenseObj); if (ld != null) result._Licenses.put(ld.getId(), ld); } catch (JSONException e) { LOGW(TAG, "Error reading LicenseInfo", e); } } JSONArray packages = null; try { packages = obj.getJSONArray(PACKAGE_ARRAY_IDENTIFIER); } catch (JSONException e) { LOGW(TAG, "Error reading LicenseInfo", e); return null; } for (int i = 0; i < packages.length(); i++) { try { JSONObject packageObj = packages.getJSONObject(i); PackageInfo pi = PackageInfo.readFromJSON(packageObj, result); if (pi != null) result._Packages.add(pi); } catch (JSONException e) { LOGW(TAG, "Error reading LicenseInfo", e); } } return result; }
From source file:com.pimp.companionforband.fragments.cloud.SummariesFragment.java
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); summariesLV = (ListView) view.findViewById(R.id.summaries_listview); statusTV = (TextView) view.findViewById(R.id.status_textview); stringArrayList = new ArrayList<>(); stringArrayAdapter = new ArrayAdapter<>(getContext(), R.layout.activities_list_item, R.id.list_item_textView, stringArrayList); summariesLV.setAdapter(stringArrayAdapter); RequestQueue queue = Volley.newRequestQueue(getContext()); JsonObjectRequest profileRequest = new JsonObjectRequest(Request.Method.GET, CloudConstants.BASE_URL + CloudConstants.Summaries_URL + "Daily?startTime=2015-01-01T16%3A04%3A49.8578590-07%3A00", null, new Response.Listener<JSONObject>() { @Override//ww w. j av a 2 s .c o m public void onResponse(JSONObject response) { statusTV.setText("CSV file can be found in CompanionForBand/Summaries\n"); Iterator<String> stringIterator = response.keys(); while (stringIterator.hasNext()) { try { String key = stringIterator.next(); JSONArray jsonArray = response.getJSONArray(key); String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "CompanionForBand" + File.separator + "Summaries"; File file = new File(path); file.mkdirs(); JsonFlattener parser = new JsonFlattener(); CSVWriter writer = new CSVWriter(); try { List<LinkedHashMap<String, String>> flatJson = parser .parseJson(jsonArray.toString()); writer.writeAsCSV(flatJson, path + File.separator + key + ".csv"); } catch (Exception e) { Log.e("SummariesParseJson", e.toString()); } for (int i = 0; i < jsonArray.length(); i++) { JSONObject activity = jsonArray.getJSONObject(i); Iterator<String> iterator = activity.keys(); String str = ""; while (iterator.hasNext()) { key = iterator.next(); str = str + UIUtils.splitCamelCase(key) + " : " + activity.get(key).toString() + "\n"; } stringArrayAdapter.add(str); } } catch (Exception e) { Log.e("Summaries", e.toString()); } } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show(); } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<>(); headers.put("Authorization", "Bearer " + MainActivity.sharedPreferences.getString("access_token", "hi")); return headers; } }; queue.add(profileRequest); }
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * Takes a JSON contact object and loops through the available organizations. If the * organization has an id that is not equal to null the organization will be updated in the database. * If the id is null then we treat it as a new organization. * //w w w . java 2 s . c o m * @param contact the contact to extract the organizations from * @param uri the base URI for this contact. */ private void saveOrganizations(JSONObject contact, Uri newPersonUri) { ContentValues values = new ContentValues(); Uri orgUri = Uri.withAppendedPath(newPersonUri, Contacts.Organizations.CONTENT_DIRECTORY); String id = null; try { JSONArray orgs = contact.getJSONArray("organizations"); if (orgs != null && orgs.length() > 0) { JSONObject org; for (int i = 0; i < orgs.length(); i++) { org = orgs.getJSONObject(i); id = getJsonString(org, "id"); values.put(Contacts.Organizations.COMPANY, getJsonString(org, "name")); values.put(Contacts.Organizations.TITLE, getJsonString(org, "title")); if (id == null) { Uri contactUpdate = mApp.getContentResolver().insert(orgUri, values); } else { Uri tempUri = Uri.withAppendedPath(orgUri, id); mApp.getContentResolver().update(tempUri, values, null, null); } } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not save organizations = " + e.getMessage()); } }
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * Takes a JSON contact object and loops through the available addresses. If the * address has an id that is not equal to null the address will be updated in the database. * If the id is null then we treat it as a new address. * /*from ww w .j av a 2 s. co m*/ * @param contact the contact to extract the addresses from * @param uri the base URI for this contact. */ private void saveAddresses(JSONObject contact, Uri uri) { ContentValues values = new ContentValues(); Uri newUri = Uri.withAppendedPath(uri, Contacts.People.ContactMethods.CONTENT_DIRECTORY); String id = null; try { JSONArray entries = contact.getJSONArray("addresses"); if (entries != null && entries.length() > 0) { JSONObject entry; values.put(Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL); for (int i = 0; i < entries.length(); i++) { entry = entries.getJSONObject(i); id = getJsonString(entry, "id"); String address = getJsonString(entry, "formatted"); if (address != null) { values.put(Contacts.ContactMethods.DATA, address); } else { values.put(Contacts.ContactMethods.DATA, createAddressString(entry)); } if (id == null) { Uri contactUpdate = mApp.getContentResolver().insert(newUri, values); } else { Uri tempUri = Uri.withAppendedPath(newUri, id); mApp.getContentResolver().update(tempUri, values, null, null); } } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not save address = " + e.getMessage()); } }
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * Takes a JSON contact object and loops through the available entries (Emails/IM's). If the * entry has an id that is not equal to null the entry will be updated in the database. * If the id is null then we treat it as a new entry. * /*from w w w . j av a 2 s. co m*/ * @param contact the contact to extract the entries from * @param uri the base URI for this contact. */ private void saveEntries(JSONObject contact, Uri uri, String dataType, int contactKind) { ContentValues values = new ContentValues(); Uri newUri = Uri.withAppendedPath(uri, Contacts.People.ContactMethods.CONTENT_DIRECTORY); String id = null; try { JSONArray entries = contact.getJSONArray(dataType); if (entries != null && entries.length() > 0) { JSONObject entry; values.put(Contacts.ContactMethods.KIND, contactKind); for (int i = 0; i < entries.length(); i++) { entry = entries.getJSONObject(i); id = getJsonString(entry, "id"); values.put(Contacts.ContactMethods.DATA, getJsonString(entry, "value")); values.put(Contacts.ContactMethods.TYPE, getContactType(getJsonString(entry, "type"))); if (id == null) { Uri contactUpdate = mApp.getContentResolver().insert(newUri, values); } else { Uri tempUri = Uri.withAppendedPath(newUri, id); mApp.getContentResolver().update(tempUri, values, null, null); } } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not save " + dataType + " = " + e.getMessage()); } }
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * Takes a JSON contact object and loops through the available phone numbers. If the phone * number has an id that is not equal to null the phone number will be updated in the database. * If the id is null then we treat it as a new phone number. * /*w w w .j av a2 s . c om*/ * @param contact the contact to extract the phone numbers from * @param uri the base URI for this contact. */ private void savePhoneNumbers(JSONObject contact, Uri uri) { ContentValues values = new ContentValues(); Uri phonesUri = Uri.withAppendedPath(uri, Contacts.People.Phones.CONTENT_DIRECTORY); String id = null; try { JSONArray phones = contact.getJSONArray("phoneNumbers"); if (phones != null && phones.length() > 0) { JSONObject phone; for (int i = 0; i < phones.length(); i++) { phone = phones.getJSONObject(i); id = getJsonString(phone, "id"); values.put(Contacts.Phones.NUMBER, getJsonString(phone, "value")); values.put(Contacts.Phones.TYPE, getPhoneType(getJsonString(phone, "type"))); if (id == null) { Uri phoneUpdate = mApp.getContentResolver().insert(phonesUri, values); } else { Uri newUri = Uri.withAppendedPath(phonesUri, id); mApp.getContentResolver().update(newUri, values, null, null); } } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not save phones = " + e.getMessage()); } }
From source file:org.jsharkey.grouphome.LauncherActivity.java
protected String resolveGroup(JSONObject groupMap, String packageName) { try {/*from ww w . j av a 2s.c o m*/ for (Iterator keys = groupMap.keys(); keys.hasNext();) { String groupName = (String) keys.next(); JSONArray packages = groupMap.getJSONArray(groupName); for (int i = 0; i < packages.length(); i++) { if (packageName.equals(packages.getString(i))) return groupName; } } } catch (Exception e) { Log.e(TAG, "Problem while trying to resolve group", e); } return GROUP_UNKNOWN; }
From source file:Technique.JsonGetInfoPos.java
public static void main(String[] args) throws IOException, JSONException { JSONObject json = readJsonFromUrl( "http://maps.googleapis.com/maps/api/geocode/json?latlng=37.215217,10.127766&sensor=false"); try {/*from ww w. j av a 2 s . c o m*/ JSONArray jArr = json.getJSONArray("results"); JSONObject json1 = jArr.getJSONObject(0); System.out.println((json1.getString("formatted_address"))); } catch (JSONException e) { e.printStackTrace(); } }
From source file:Technique.JsonGetInfoPos.java
public String getInfo(String pos) throws IOException, JSONException { JSONObject json = readJsonFromUrl( "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + pos + "&sensor=false"); try {/*w w w .j a va 2 s. c o m*/ JSONArray jArr = json.getJSONArray("results"); JSONObject json1 = jArr.getJSONObject(0); System.out.println((json1.getString("formatted_address"))); return json1.getString("formatted_address"); } catch (JSONException e) { e.printStackTrace(); } return ""; }
From source file:com.yayandroid.utility.MapHelperFragment.java
/** * GoogleApis returns an address as jsonObject according to given * coordinate, and here we parse it to find necessary fields *//*ww w . jav a 2s . co m*/ private void fetchInformationUsingGoogleMap() { final AndroidHttpClient ANDROID_HTTP_CLIENT = AndroidHttpClient .newInstance(MapHelperFragment.class.getName()); String googleMapUrl = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + myLocation.getLatitude() + "," + myLocation.getLongitude() + "&sensor=false&language=tr"; try { JSONObject googleMapResponse = new JSONObject( ANDROID_HTTP_CLIENT.execute(new HttpGet(googleMapUrl), new BasicResponseHandler())); // many nested loops.. not great -> use expression instead // loop among all results JSONArray results = (JSONArray) googleMapResponse.get("results"); for (int i = 0; i < results.length(); i++) { // loop among all addresses within this result JSONObject result = results.getJSONObject(i); if (result.has("address_components")) { JSONArray addressComponents = result.getJSONArray("address_components"); for (int j = 0; j < addressComponents.length(); j++) { JSONObject addressComponent = addressComponents.getJSONObject(j); if (result.has("types")) { JSONArray types = addressComponent.getJSONArray("types"); for (int k = 0; k < requiredInformations.length; k++) { for (int l = 0; l < types.length(); l++) { if (requiredInformations[k].type.value.equals(types.getString(l))) { if (addressComponent.has("long_name")) { PostInformation(requiredInformations[k].type, addressComponent.getString("long_name")); } else if (addressComponent.has("short_name")) { PostInformation(requiredInformations[k].type, addressComponent.getString("short_name")); } } } } } } } } } catch (Exception ignored) { ignored.printStackTrace(); } ANDROID_HTTP_CLIENT.close(); }