List of usage examples for org.json JSONObject has
public boolean has(String key)
From source file:com.snappy.couchdb.CouchDBHelper.java
/** * Find all messages for current user//from w w w . jav a 2 s. c o m * * @param json * @return * @throws JSONException * @throws SpikaException * @throws IOException * @throws ClientProtocolException */ public static ArrayList<Message> findMessagesForUser(JSONObject json) throws JSONException, ClientProtocolException, IOException, SpikaException { ArrayList<Message> messages = null; if (json != null) { if (json.has(Const.ERROR)) { appLogout(null, false, isInvalidToken(json)); return null; } messages = new ArrayList<Message>(); JSONArray rows = json.getJSONArray(Const.ROWS); for (int i = 0; i < rows.length(); i++) { JSONObject row = rows.getJSONObject(i); JSONObject msgJson = row.getJSONObject(Const.VALUE); Message message = null; String messageType = msgJson.getString(Const.MESSAGE_TYPE); if (messageType.equals(Const.TEXT)) { message = new Gson().fromJson(msgJson.toString(), Message.class); } else if (messageType.equals(Const.IMAGE)) { message = parseMessageObject(msgJson, true, false, false); } else if (messageType.equals(Const.VOICE)) { message = parseMessageObject(msgJson, false, true, false); } else if (messageType.equals(Const.VIDEO)) { message = parseMessageObject(msgJson, false, false, true); } else if (messageType.equals(Const.EMOTICON)) { message = parseMessageObject(msgJson, false, false, false); } else { message = new Gson().fromJson(msgJson.toString(), Message.class); } if (message == null) { continue; } else { messages.add(message); } } } if (null != messages) { Collections.sort(messages); } return messages; }
From source file:com.snappy.couchdb.CouchDBHelper.java
/** * Parse a single JSON object of Message type * //from w ww . j av a2 s . c o m * @param json * @param image * @param voice * @return * @throws SpikaException * @throws JSONException * @throws IOException * @throws ClientProtocolException */ private static Message parseMessageObject(JSONObject json, boolean image, boolean voice, boolean video) throws ClientProtocolException, IOException, JSONException, SpikaException { Message message = new Message(); if (json == null) { return message; } if (json.has(Const.ERROR)) { appLogout(null, false, isInvalidToken(json)); return null; } try { message.setId(json.getString(Const._ID)); } catch (JSONException e) { message.setId(""); } try { message.setRev(json.getString(Const._REV)); } catch (JSONException e) { message.setRev(""); } try { message.setType(json.getString(Const.TYPE)); } catch (JSONException e) { message.setType(""); } try { message.setMessageType(json.getString(Const.MESSAGE_TYPE)); } catch (JSONException e) { message.setMessageType(""); } try { message.setMessageTargetType(json.getString(Const.MESSAGE_TARGET_TYPE)); } catch (JSONException e) { message.setMessageTargetType(""); } try { message.setBody(json.getString(Const.BODY)); } catch (JSONException e) { message.setBody(""); } try { message.setFromUserId(json.getString(Const.FROM_USER_ID)); } catch (JSONException e) { message.setFromUserId(""); } try { message.setFromUserName(json.getString(Const.FROM_USER_NAME)); } catch (JSONException e) { message.setFromUserName(""); } try { message.setToUserId(json.getString(Const.TO_USER_ID)); } catch (JSONException e) { message.setToUserId(""); } try { message.setToGroupName(json.getString(Const.TO_USER_NAME)); } catch (JSONException e) { message.setToGroupName(""); } try { message.setToGroupId(json.getString(Const.TO_GROUP_ID)); } catch (JSONException e) { message.setToGroupId(""); } try { message.setToGroupName(json.getString(Const.TO_GROUP_NAME)); } catch (JSONException e) { message.setToGroupName(""); } try { message.setCreated(json.getLong(Const.CREATED)); } catch (JSONException e) { return null; } try { message.setModified(json.getLong(Const.MODIFIED)); } catch (JSONException e) { return null; } try { message.setValid(json.getBoolean(Const.VALID)); } catch (JSONException e) { message.setValid(true); } try { message.setAttachments(json.getJSONObject(Const.ATTACHMENTS).toString()); } catch (JSONException e) { message.setAttachments(""); } try { message.setLatitude(json.getString(Const.LATITUDE)); } catch (JSONException e) { message.setLatitude(""); } try { message.setLongitude(json.getString(Const.LONGITUDE)); } catch (JSONException e) { message.setLongitude(""); } try { message.setImageFileId((json.getString(Const.PICTURE_FILE_ID))); } catch (JSONException e) { message.setImageFileId(""); } try { message.setImageThumbFileId((json.getString(Const.PICTURE_THUMB_FILE_ID))); } catch (JSONException e) { message.setImageThumbFileId(""); } try { message.setVideoFileId((json.getString(Const.VIDEO_FILE_ID))); } catch (JSONException e) { message.setVideoFileId(""); } try { message.setVoiceFileId((json.getString(Const.VOICE_FILE_ID))); } catch (JSONException e) { message.setVoiceFileId(""); } try { message.setEmoticonImageUrl(json.getString(Const.EMOTICON_IMAGE_URL)); } catch (JSONException e) { message.setEmoticonImageUrl(""); } try { message.setAvatarFileId(json.getString(Const.AVATAR_THUMB_FILE_ID)); } catch (JSONException e) { message.setAvatarFileId(""); } try { message.setDeleteType(json.getInt(Const.DELETE_TYPE)); } catch (JSONException e) { message.setDeleteType(0); } try { message.setDelete(json.getInt(Const.DELETE_AT)); } catch (JSONException e) { message.setDelete(0); } try { message.setReadAt(json.getInt(Const.READ_AT)); } catch (JSONException e) { message.setReadAt(0); } try { message.setCommentCount(json.getInt(Const.COMMENT_COUNT)); } catch (JSONException e) { message.setCommentCount(0); } // if (image || video || voice) { // message.setCommentCount(CouchDB.getCommentCount(message.getId())); // } return message; }
From source file:com.snappy.couchdb.CouchDBHelper.java
/** * Parse comments Json/*from w ww .jav a 2s . co m*/ * * @param json * @return * @throws JSONException */ public static List<Comment> parseCommentsJson(JSONObject json) throws JSONException { List<Comment> comments = null; if (json != null) { if (json.has(Const.ERROR)) { appLogout(null, false, isInvalidToken(json)); return null; } comments = new ArrayList<Comment>(); JSONArray rows = json.getJSONArray(Const.COMMENTS); for (int i = 0; i < rows.length(); i++) { JSONObject commentJson = rows.getJSONObject(i); Comment comment = sGsonExpose.fromJson(commentJson.toString(), Comment.class); comments.add(comment); } } return comments; }
From source file:com.snappy.couchdb.CouchDBHelper.java
/** * Parse multi comment objects/*from w w w .j a v a2 s . c o m*/ * * @param json * @return * @throws JSONException */ public static List<Comment> parseMultiCommentObjects(JSONObject json) throws JSONException { List<Comment> comments = null; if (json != null) { if (json.has(Const.ERROR)) { appLogout(null, false, isInvalidToken(json)); return null; } comments = new ArrayList<Comment>(); JSONArray rows = json.getJSONArray(Const.ROWS); for (int i = 0; i < rows.length(); i++) { JSONObject row = rows.getJSONObject(i); String key = row.getString(Const.KEY); if (!"null".equals(key)) { JSONObject commentJson = row.getJSONObject(Const.VALUE); Comment comment = sGsonExpose.fromJson(commentJson.toString(), Comment.class); comments.add(comment); } } } return comments; }
From source file:com.snappy.couchdb.CouchDBHelper.java
/** * Parse multi emoticon objects/* w w w. ja v a 2 s .c om*/ * * @param json * @return * @throws JSONException */ public static List<Emoticon> parseMultiEmoticonObjects(JSONObject json) throws JSONException { List<Emoticon> emoticons = null; if (json != null) { if (json.has(Const.ERROR)) { appLogout(null, false, isInvalidToken(json)); return null; } emoticons = new ArrayList<Emoticon>(); JSONArray rows = json.getJSONArray(Const.ROWS); for (int i = 0; i < rows.length(); i++) { JSONObject row = rows.getJSONObject(i); String key = row.getString(Const.KEY); if (!"null".equals(key)) { JSONObject emoticonJson = row.getJSONObject(Const.VALUE); Emoticon emoticon = sGsonExpose.fromJson(emoticonJson.toString(), Emoticon.class); emoticons.add(emoticon); // SpikaApp.getFileDir().saveFile( // emoticon.getIdentifier(), // emoticon.getImageUrl()); } } } return emoticons; }
From source file:com.snappy.couchdb.CouchDBHelper.java
private static boolean isInvalidToken(JSONObject json) { if (json.has(Const.MESSAGE)) { try {/*w ww. j a v a 2 s .c o m*/ String errorMessage = json.getString(Const.MESSAGE); if (errorMessage.equalsIgnoreCase(Const.INVALID_TOKEN)) { return true; } } catch (JSONException e) { e.printStackTrace(); } } return false; }
From source file:com.mercandalli.android.apps.files.main.Config.java
private void load(final Context context) { final String fileContent = FileUtils.readStringFile(context, FILE_NAME); if (fileContent == null) { return;//from w ww. j a v a 2 s. c om } try { JSONObject tmpJson = new JSONObject(fileContent); if (tmpJson.has("settings_1")) { JSONObject tmpSettings1 = tmpJson.getJSONObject("settings_1"); for (ENUM_Int enum_int : ENUM_Int.values()) { if (tmpSettings1.has(enum_int.key)) { enum_int.value = tmpSettings1.getInt(enum_int.key); } } for (ENUM_Boolean enum_boolean : ENUM_Boolean.values()) { if (tmpSettings1.has(enum_boolean.key)) { enum_boolean.value = tmpSettings1.getBoolean(enum_boolean.key); } } for (ENUM_String enum_string : ENUM_String.values()) { if (tmpSettings1.has(enum_string.key)) { enum_string.value = tmpSettings1.getString(enum_string.key); } } } } catch (JSONException e) { Log.e(getClass().getName(), "Failed to convert Json", e); } }
From source file:com.whizzosoftware.hobson.dto.property.PropertyContainerSetDTO.java
private PropertyContainerSetDTO(JSONObject json, PropertyContainerMappingContext context) { String propertyListName = context.getContainersName(); if (propertyListName == null) { propertyListName = JSONAttributes.CONTAINERS; }/*from www .jav a 2 s . co m*/ if (json.has(JSONAttributes.AID)) { setId(json.getString(JSONAttributes.AID)); } if (json.has(propertyListName)) { containers = new ArrayList<>(); JSONArray ja = json.getJSONArray(propertyListName); for (int i = 0; i < ja.length(); i++) { containers.add(new PropertyContainerDTO.Builder(ja.getJSONObject(i)).build()); } } }
From source file:com.imaginary.home.cloud.device.PoweredDevice.java
static public void mapPoweredDevice(@Nonnull ControllerRelay relay, @Nonnull JSONObject json, @Nonnull Map<String, Object> state) throws JSONException { mapDevice(relay, json, state);/*from ww w.j av a 2 s . c o m*/ state.put("deviceType", "powered"); state.put("on", json.has("on") && !json.isNull("on") && json.getBoolean("on")); }
From source file:com.att.voice.AttDigitalLife.java
public Map<String, String> authtokens() { Map<String, String> authMap = new HashMap<>(); String json = ""; try {/*w w w. java2 s.c o m*/ URIBuilder builder = new URIBuilder(); builder.setScheme(HTTP_PROTOCOL).setHost(DIGITAL_LIFE_PATH).setPath("/penguin/api/authtokens") .setParameter(USER_ID_PARAMETER, username).setParameter(PASSWORD_PARAMETER, password) .setParameter(DOMAIN_PARAMETER, "DL").setParameter(APP_KEY_PARAMETER, APP_KEY); URI uri = builder.build(); HttpPost httpPost = new HttpPost(uri); HttpResponse httpResponse = httpclient.execute(httpPost); httpResponse.getEntity(); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { json = EntityUtils.toString(entity); } JSONObject jsonObject = new JSONObject(json); JSONObject content = jsonObject.getJSONObject("content"); authMap.put("id", content.getJSONArray("gateways").getJSONObject(0).getString("id")); authMap.put("Authtoken", content.getString("authToken")); authMap.put("Requesttoken", content.getString("requestToken")); authMap.put("Appkey", APP_KEY); if (content.has("contact") && content.getJSONObject("contact").has("firstName") && content.getJSONObject("contact").has("lastName")) { authMap.put("name", content.getJSONObject("contact").getString("firstName") + " " + content.getJSONObject("contact").getString("lastName")); } return authMap; } catch (IOException | URISyntaxException ex) { Logger.getLogger(AttDigitalLife.class.getName()).log(Level.SEVERE, null, ex); } return null; }