List of usage examples for org.json JSONObject has
public boolean has(String key)
From source file:org.brickred.socialauth.provider.GoogleOAuth2Impl.java
private Profile authGoogleLogin() throws Exception { String presp;//from ww w .j av a 2 s . com try { Response response = authenticationStrategy.executeFeed(PROFILE_URL); presp = response.getResponseBodyAsString(Constants.ENCODING); } catch (Exception e) { throw new SocialAuthException("Error while getting profile from " + PROFILE_URL, e); } try { logger.debug("User Profile : " + presp); JSONObject resp = new JSONObject(presp); Profile p = new Profile(); p.setValidatedId(resp.getString("id")); p.setFirstName(resp.getString("given_name")); p.setLastName(resp.getString("family_name")); p.setEmail(resp.getString("email")); if (resp.has("location")) { p.setLocation(resp.getJSONObject("location").getString("name")); } if (resp.has("birthday")) { String bstr = resp.getString("birthday"); String[] arr = bstr.split("-"); BirthDate bd = new BirthDate(); if (arr.length > 0) { bd.setYear(Integer.parseInt(arr[2])); } if (arr.length > 1) { bd.setMonth(Integer.parseInt(arr[0])); } if (arr.length > 2) { bd.setDay(Integer.parseInt(arr[1])); } p.setDob(bd); } if (resp.has("gender")) { p.setGender(resp.getString("gender")); } if (resp.has("picture")) { p.setProfileImageURL(resp.getString("picture")); } if (resp.has("locale")) { String locale = resp.getString("locale"); if (locale != null) { String a[] = locale.split("-"); p.setLanguage(a[0]); if (a.length > 1) { p.setCountry(a[1]); } } } p.setProviderId(getProviderId()); userProfile = p; return p; } catch (Exception ex) { throw new ServerDataException("Failed to parse the user profile json : " + presp, ex); } }
From source file:org.protorabbit.Config.java
static void processURIResources(int type, JSONObject bsjo, ITemplate temp, String baseURI) throws JSONException { List<ResourceURI> refs = null; refs = new ArrayList<ResourceURI>(); if (bsjo.has("libs")) { JSONArray ja = bsjo.getJSONArray("libs"); for (int j = 0; j < ja.length(); j++) { JSONObject so = ja.getJSONObject(j); String url = so.getString("url"); if (url.startsWith("/") || url.startsWith("http")) { baseURI = ""; }// w w w.jav a 2s.c om ResourceURI ri = new ResourceURI(url, baseURI, type); if (so.has("id")) { ri.setId(so.getString("id")); } if (so.has("uaTest")) { ri.setUATest(so.getString("uaTest")); } if (so.has("test")) { ri.setTest(so.getString("test")); } if (so.has("defer")) { ri.setDefer(so.getBoolean("defer")); } if (so.has("combine")) { ri.setCombine(so.getBoolean("combine")); } if (so.has("uniqueURL")) { Boolean unique = so.getBoolean("uniqueURL"); ri.setUniqueURL(unique); } refs.add(ri); } } Boolean combine = null; if (bsjo.has("combineResources")) { combine = bsjo.getBoolean("combineResources"); } Boolean lgzip = null; if (bsjo.has("gzip")) { lgzip = bsjo.getBoolean("gzip"); } if (type == ResourceURI.SCRIPT) { temp.setCombineScripts(combine); temp.setGzipScripts(lgzip); temp.setScripts(refs); } else if (type == ResourceURI.LINK) { temp.setGzipStyles(lgzip); temp.setStyles(refs); temp.setCombineStyles(combine); } }
From source file:org.protorabbit.Config.java
@SuppressWarnings("unchecked") private void registerTemplates(ITemplate temp, JSONObject t, String baseURI) { try {/*from ww w.j av a2s. co m*/ if (t.has("timeout")) { long templateTimeout = t.getLong("timeout"); temp.setTimeout(templateTimeout); } boolean tgzip = false; if (!devMode) { tgzip = gzip; } if (t.has("gzip")) { tgzip = t.getBoolean("gzip"); temp.setGzipStyles(tgzip); temp.setGzipScripts(tgzip); temp.setGzipTemplate(tgzip); } if (t.has("uniqueURL")) { Boolean unique = t.getBoolean("uniqueURL"); temp.setUniqueURL(unique); } // template overrides default combineResources if (t.has("combineResources")) { boolean combineResources = t.getBoolean("combineResources"); temp.setCombineResources(combineResources); temp.setCombineScripts(combineResources); temp.setCombineStyles(combineResources); } if (t.has("template")) { String turi = t.getString("template"); ResourceURI templateURI = new ResourceURI(turi, baseURI, ResourceURI.TEMPLATE); temp.setTemplateURI(templateURI); } if (t.has("namespace")) { temp.setURINamespace(t.getString("namespace")); } if (t.has("extends")) { List<String> ancestors = null; String base = t.getString("extends"); if (base.length() > 0) { String[] parentIds = null; if (base.indexOf(",") != -1) { parentIds = base.split(","); } else { parentIds = new String[1]; parentIds[0] = base; } ancestors = new ArrayList<String>(); for (int j = 0; j < parentIds.length; j++) { ancestors.add(parentIds[j].trim()); } } temp.setAncestors(ancestors); } if (t.has("overrides")) { List<TemplateOverride> overrides = new ArrayList<TemplateOverride>(); JSONArray joa = t.getJSONArray("overrides"); for (int z = 0; z < joa.length(); z++) { TemplateOverride tor = new TemplateOverride(); JSONObject toro = joa.getJSONObject(z); if (toro.has("test")) { tor.setTest(toro.getString("test")); } if (toro.has("uaTest")) { tor.setUATest(toro.getString("uaTest")); } if (toro.has("import")) { tor.setImportURI(toro.getString("import")); } overrides.add(tor); } temp.setTemplateOverrides(overrides); } if (t.has("scripts")) { JSONObject bsjo = t.getJSONObject("scripts"); processURIResources(ResourceURI.SCRIPT, bsjo, temp, baseURI); } if (t.has("styles")) { JSONObject bsjo = t.getJSONObject("styles"); processURIResources(ResourceURI.LINK, bsjo, temp, baseURI); } if (t.has("properties")) { Map<String, IProperty> properties = null; JSONObject po = t.getJSONObject("properties"); properties = new HashMap<String, IProperty>(); Iterator<String> jit = po.keys(); while (jit.hasNext()) { String name = jit.next(); JSONObject so = po.getJSONObject(name); int type = IProperty.STRING; String value = so.getString("value"); if (so.has("type")) { String typeString = so.getString("type"); if ("string".equals(typeString.toLowerCase())) { type = IProperty.STRING; } else if ("include".equals(typeString.toLowerCase())) { type = IProperty.INCLUDE; } } IProperty pi = new Property(name, value, type, baseURI, temp.getId()); if (so.has("timeout")) { long timeout = so.getLong("timeout"); pi.setTimeout(timeout); } if (so.has("id")) { pi.setId(so.getString("id")); } if (so.has("uaTest")) { pi.setUATest(so.getString("uaTest")); } if (so.has("test")) { pi.setTest(so.getString("test")); } if (so.has("defer")) { pi.setDefer(so.getBoolean("defer")); } if (so.has("deferContent")) { pi.setDeferContent(new StringBuffer(so.getString("deferContent"))); } properties.put(name, pi); } temp.setProperties(properties); } } catch (JSONException e) { getLogger().log(Level.SEVERE, "Error parsing configuration.", e); } }
From source file:org.protorabbit.Config.java
public String getTemplateURI(JSONObject template) { if (template.has("template")) { try {/* w ww. ja v a 2 s. c o m*/ return template.getString("template"); } catch (JSONException e) { getLogger().log(Level.SEVERE, "Error locating template.", e); } } return null; }
From source file:org.protorabbit.Config.java
public String getIncludeFileName(JSONObject template, String tid) { if (template.has("properties")) { try {//from ww w . java 2s .c o m JSONObject properties = template.getJSONObject("properties"); if (properties.has(tid)) { JSONObject to = properties.getJSONObject(tid); return to.getString("value"); } } catch (JSONException e) { getLogger().log(Level.SEVERE, "Error parsing include file name.", e); } } return null; }
From source file:net.jmhertlein.mcanalytics.console.gui.LoginPane.java
private void loadHostPanes(JSONObject config) { if (config.has("hosts")) { JSONArray hosts = config.getJSONArray("hosts"); for (int i = 0; i < hosts.length(); i++) { JSONObject host = hosts.getJSONObject(i); HostEntry entry = HostEntry.fromJSON(host); try { entry.setHasCert(trust.containsAlias(entry.getUrl() + "-private")); } catch (KeyStoreException ex) { Logger.getLogger(LoginPane.class.getName()).log(Level.SEVERE, null, ex); }//from w ww .j av a2s. c o m hostList.getItems().add(entry); } } if (!hostList.getItems().isEmpty()) hostList.getSelectionModel().select(0); }
From source file:com.scvngr.levelup.core.net.request.factory.ClaimRequestFactoryTest.java
@SmallTest public void testBuildClaimLegacyLoyaltyRequest() throws BadRequestException, JSONException { final String loyaltyId = "2222"; final LevelUpRequest request = (LevelUpRequest) new ClaimRequestFactory(getContext(), new MockAccessTokenRetriever()).buildClaimLegacyLoyaltyRequest(1, loyaltyId); assertEquals(HttpMethod.POST, request.getMethod()); final JSONObject object = new JSONObject(request.getBody(getContext())) .getJSONObject(ClaimRequestFactory.OUTER_PARAM_LEGACY_LOYALTY); assertTrue(object.has(ClaimRequestFactory.PARAM_LEGACY_ID)); assertEquals(loyaltyId, object.get(ClaimRequestFactory.PARAM_LEGACY_ID)); assertFalse(0 == request.getBodyLength(getContext())); final URL url = request.getUrl(getContext()); assertNotNull(url);/*from w w w .ja va 2 s. co m*/ // Make sure we hit the proper API version and url. assertEquals("/v15/loyalties/legacy/1/claims", url.getPath()); }
From source file:com.aerhard.oxygen.plugin.dbtagger.util.JsonUtil.java
/** * Transforms a JSON string to a TableData object. * /*from www .j a v a 2 s .c o m*/ * @param input * the input data * @return the table data object or null if data could not be tranformed for * further processing. */ public TableData getTableData(String input) { try { JSONObject responseJSON = new JSONObject(input); String[] headers = getTableHeaders(responseJSON); if (headers != null) { if (!responseJSON.has("data")) { return new TableData(headers, null); } String[][] data = getTableBody(responseJSON, headers.length); return (data == null) ? null : new TableData(headers, data); } } catch (Exception e) { workspace.showErrorMessage(i18n.getString("jsonUtil.transformError")); } return null; }
From source file:com.easemob.chatuidemo.DemoHXSDKHelper.java
public boolean isRobotMenuMessage(EMMessage message) { try {/*from w ww . jav a 2s. co m*/ JSONObject jsonObj = message.getJSONObjectAttribute(Constant.MESSAGE_ATTR_ROBOT_MSGTYPE); if (jsonObj.has("choice")) { return true; } } catch (Exception e) { } return false; }
From source file:com.easemob.chatuidemo.DemoHXSDKHelper.java
public String getRobotMenuMessageDigest(EMMessage message) { String title = ""; try {//from ww w.jav a 2s .co m JSONObject jsonObj = message.getJSONObjectAttribute(Constant.MESSAGE_ATTR_ROBOT_MSGTYPE); if (jsonObj.has("choice")) { JSONObject jsonChoice = jsonObj.getJSONObject("choice"); title = jsonChoice.getString("title"); } } catch (Exception e) { } return title; }