List of usage examples for org.json JSONObject names
public JSONArray names()
From source file:net.mandaria.radioreddit.apis.RadioRedditAPI.java
public static RadioStreams GetStreams(Context context, RadioRedditApplication application) { RadioStreams radiostreams = new RadioStreams(); radiostreams.ErrorMessage = ""; radiostreams.RadioStreams = new ArrayList<RadioStream>(); try {/*from w ww .jav a2 s .co m*/ String url = context.getString(R.string.radio_reddit_streams); String outputStreams = ""; boolean errorGettingStreams = false; try { outputStreams = HTTPUtil.get(context, url); } catch (Exception ex) { errorGettingStreams = true; radiostreams.ErrorMessage = context.getString(R.string.error_RadioRedditServerIsDownNotification); application.radioRedditIsDownErrorMessage = radiostreams.ErrorMessage; application.isRadioRedditDown = true; } if (!errorGettingStreams && outputStreams.length() > 0) { JSONTokener tokener = new JSONTokener(outputStreams); JSONObject json = new JSONObject(tokener); JSONObject streams = json.getJSONObject("streams"); JSONArray streams_names = streams.names(); ArrayList<RadioStream> list_radiostreams = new ArrayList<RadioStream>(); // loop through each stream for (int i = 0; i < streams.length(); i++) { String name = streams_names.getString(i); JSONObject stream = streams.getJSONObject(name); RadioStream radiostream = new RadioStream(); radiostream.Name = name; // if(stream.has("type")) radiostream.Type = stream.getString("type"); radiostream.Description = stream.getString("description"); radiostream.Status = stream.getString("status"); // call status.json to get Relay // form url radioreddit.com + status + json String status_url = context.getString(R.string.radio_reddit_base_url) + radiostream.Status + context.getString(R.string.radio_reddit_status); String outputStatus = ""; boolean errorGettingStatus = false; try { outputStatus = HTTPUtil.get(context, status_url); } catch (Exception ex) { errorGettingStatus = true; radiostreams.ErrorMessage = context .getString(R.string.error_RadioRedditServerIsDownNotification); } //Log.e("RadioReddit", "Length of output: "+ outputStatus.length() + "; Content of output: " + outputStatus); // TODO: does outputStatus.length() > 0 need to be checked here and return a ErrorMessage back and set ErrorGettingStatus = true? if (!errorGettingStatus && outputStatus.length() > 0) { JSONTokener status_tokener = new JSONTokener(outputStatus); JSONObject status_json = new JSONObject(status_tokener); radiostream.Online = Boolean.parseBoolean(status_json.getString("online").toLowerCase()); if (radiostream.Online == true) // if offline, no other nodes are available { radiostream.Relay = status_json.getString("relay"); list_radiostreams.add(radiostream); } } } // JSON parsing reverses the list for some reason, fixing it... if (list_radiostreams.size() > 0) { // Sorting will happen later on select station activity //Collections.reverse(list_radiostreams); radiostreams.RadioStreams = list_radiostreams; application.isRadioRedditDown = false; } else { radiostreams.ErrorMessage = context.getString(R.string.error_NoStreams); application.radioRedditIsDownErrorMessage = radiostreams.ErrorMessage; application.isRadioRedditDown = true; } } } catch (Exception ex) { // We fail to get the streams... CustomExceptionHandler ceh = new CustomExceptionHandler(context); ceh.sendEmail(ex); radiostreams.ErrorMessage = ex.toString(); ex.printStackTrace(); } return radiostreams; }
From source file:ch.lom.clemens.android.bibliography.data.JSONWebConnector.java
private MDocument parseDocumentResult(MDocument mdoc, String strResponse) throws JSONException { JSONObject doc = new JSONObject(strResponse); try {/*from w w w . j ava2 s. c om*/ mdoc.title = doc.getString("title"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document title"); } try { mdoc.year = doc.getString("year"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document year"); } try { mdoc.notes = doc.getString("notes"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document notes"); } try { mdoc.type = doc.getString("type"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document type"); } try { mdoc.urls = new String[] { doc.getString("url") }; } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document url"); } try { mdoc.pages = doc.getString("pages"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document pages"); } try { mdoc.docabstract = doc.getString("abstract"); } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document abstract"); } try { JSONArray authors = doc.getJSONArray("authors"); String[] strAuthors = new String[authors.length()]; for (int j = 0; j < authors.length(); j++) { strAuthors[j] = authors.getString(j); } mdoc.authors = strAuthors; } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document authors"); } try { JSONArray tags = doc.getJSONArray("tags"); String[] mtags = new String[tags.length()]; for (int j = 0; j < tags.length(); j++) { mtags[j] = tags.getString(j); } mdoc.tags = mtags; } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document tags"); } try { JSONObject ids = doc.getJSONObject("identifiers"); mdoc.identifiers = new HashMap<String, String>(); JSONArray names = ids.names(); for (int j = 0; j < names.length(); j++) { mdoc.identifiers.put(names.getString(j), ids.getString(names.getString(j))); } } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document identifiers"); } try { JSONObject ids = doc.getJSONObject("discipline"); mdoc.discipline = new HashMap<String, String>(); JSONArray names = ids.names(); for (int j = 0; j < names.length(); j++) { mdoc.discipline.put(names.getString(j), ids.getString(names.getString(j))); } } catch (JSONException e) { Log.w("WebConnector", "Parsing error while getting document disciplines"); } return mdoc; }
From source file:com.phonegap.App.java
/** * Load the url into the webview./* w ww . ja va 2 s . c o m*/ * * @param url * @param props Properties that can be passed in to the DroidGap activity (i.e. loadingDialog, wait, ...) * @throws JSONException */ public void loadUrl(String url, JSONObject props) throws JSONException { System.out.println("App.loadUrl(" + url + "," + props + ")"); int wait = 0; // If there are properties, then set them on the Activity if (props != null) { JSONArray keys = props.names(); for (int i = 0; i < keys.length(); i++) { String key = keys.getString(i); if (key.equals("wait")) { wait = props.getInt(key); } else { Object value = props.get(key); if (value == null) { } else if (value.getClass().equals(String.class)) { this.ctx.getIntent().putExtra(key, (String) value); } else if (value.getClass().equals(Boolean.class)) { this.ctx.getIntent().putExtra(key, (Boolean) value); } else if (value.getClass().equals(Integer.class)) { this.ctx.getIntent().putExtra(key, (Integer) value); } } } } // If wait property, then delay loading if (wait > 0) { ((DroidGap) this.ctx).loadUrl(url, wait); } else { ((DroidGap) this.ctx).loadUrl(url); } }
From source file:com.mobile.system.db.abatis.AbatisService.java
/** */* ww w. jav a2s .c om*/ */ public int execute(String sqlId, JSONObject json) { Map<String, Object> map = null; if (json != null) { map = new HashMap<String, Object>(); for (int j = 0; j < json.names().length(); j++) { String strKey = null; try { strKey = (String) json.names().get(j); map.put(strKey, json.get(strKey)); } catch (JSONException ex) { Logger.getLogger(AbatisService.class.getName()).log(Level.SEVERE, null, ex); } } } return execute(sqlId, map); }
From source file:com.jennifer.ui.util.DomUtil.java
public DomUtil attr(JSONObject JSONObject) { JSONArray list = JSONObject.names(); for (int i = 0, len = list.length(); i < len; i++) { String key = list.getString(i); put(key, JSONObject.get(key));//from w w w .j ava 2 s . c o m } return this; }
From source file:com.orange.oidc.secproxy_service.Token.java
public void fromToken(String token) { reset();//from w w w . ja v a 2 s .c o m if (token == null) return; try { JSONObject jObject = null; // try token as is try { jObject = new JSONObject(token); } catch (Exception e) { } // try to decode JWT if (jObject == null) { String ds = getJSON(token); if (ds != null) jObject = new JSONObject(ds); } if (jObject != null) { JSONArray names = jObject.names(); if (names != null) { for (int j = 0; j < names.length(); j++) { String name = names.getString(j); // Log.d("Token",name); setField(name, jObject.get(name)); } } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.zaizi.sensefy.api.utils.JSONHelper.java
public static boolean isEmptyObject(JSONObject object) { return object.names() == null; }
From source file:org.dasein.cloud.joyent.SmartDataCenter.java
public @Nonnull String getEndpoint() throws CloudException, InternalException { ProviderContext ctx = getContext();/*from www . ja v a 2 s .com*/ if (ctx == null) { throw new CloudException("No context has been established for this request"); } String e = ctx.getEndpoint(); if (e == null) { e = "https://us-west-1.api.joyentcloud.com"; } String[] parts = e.split(","); if (parts == null || parts.length < 1) { parts = new String[] { e }; } String r = ctx.getRegionId(); if (r == null) { return parts[0]; } if (endpointCache.containsKey(e)) { Map<String, String> cache = endpointCache.get(e); if (cache != null && cache.containsKey(r)) { String endpoint = cache.get(r); if (endpoint != null) { return endpoint; } } } JoyentMethod method = new JoyentMethod(this); String json = method.doGetJson(parts[0], "datacenters"); try { JSONObject ob = new JSONObject(json); JSONArray ids = ob.names(); for (int i = 0; i < ids.length(); i++) { String regionId = ids.getString(i); if (regionId.equals(r) && ob.has(regionId)) { String endpoint = ob.getString(regionId); Map<String, String> cache; if (endpointCache.containsKey(e)) { cache = endpointCache.get(e); } else { cache = new HashMap<String, String>(); endpointCache.put(e, cache); } cache.put(r, endpoint); return endpoint; } } throw new CloudException("No endpoint exists for " + r); } catch (JSONException ex) { throw new CloudException(ex); } }
From source file:org.chromium.ChromeI18n.java
private JSONObject toLowerCaseMessage(JSONObject contents) throws JSONException { List<String> messages = toStringList(contents.names()); for (String message : messages) { JSONObject value = contents.getJSONObject(message); contents.remove(message);/*from ww w .j a v a 2 s . c o m*/ contents.put(message.toLowerCase(), value); } return contents; }
From source file:com.roiland.crm.sm.core.service.impl.ContacterAPIImpl.java
@Override public Contacter createContacter(String userID, String dealerOrgID, Contacter contacter) throws ResponseException { // ?/* w ww . j av a 2 s .co m*/ Contacter returnContacter = null; try { if (userID == null || dealerOrgID == null) { throw new ResponseException("userID or dealerOrgID is null."); } JSONObject params = new JSONObject(); params.put("userID", userID); params.put("dealerOrgID", dealerOrgID); params.put("projectID", contacter.getProjectID()); params.put("customerID", contacter.getCustomerID()); params.put("contName", contacter.getContName()); params.put("contMobile", contacter.getContMobile()); params.put("contOtherPhone", contacter.getContOtherPhone()); params.put("isPrimContanter", contacter.getIsPrimContanter()); params.put("contGenderCode", contacter.getContGenderCode()); params.put("contBirthday", contacter.getContBirthday()); params.put("idNumber", contacter.getIdNumber()); params.put("ageScopeCode", contacter.getAgeScopeCode()); params.put("contTypeCode", contacter.getContTypeCode()); params.put("contRelationCode", contacter.getContRelationCode()); if (contacter.getLicenseValid() == 0) { params.put("licenseValid", null); } else params.put("licenseValid", contacter.getLicenseValid()); RLHttpResponse response = getHttpClient() .executePostJSON(getURLAddress(URLContact.METHOD_CREATE_CONTACTER), params, null); if (response.isSuccess()) { returnContacter = contacter; JSONObject result = new JSONObject(getSimpleString(response)); String node = null; String error = null; JSONArray nodeArray = result.names(); if (nodeArray != null) { for (int i = 0; i < nodeArray.length(); i++) { node = nodeArray.get(i).toString(); if (node.equalsIgnoreCase("success")) { Boolean success = Boolean.parseBoolean(result.getString("success")); if (success) { // ?ID returnContacter.setContacterID(result.getString("contacterID")); } } else if (node.equalsIgnoreCase("validate_error")) { error = parsingValidation(result.getJSONObject(node)); throw new ResponseException(error); } } } return returnContacter; } throw new ResponseException(); } catch (IOException e) { Log.e(tag, "Connection network error.", e); throw new ResponseException(e); } catch (JSONException e) { Log.e(tag, "Parsing data error.", e); throw new ResponseException(e); } }