Example usage for org.json JSONObject has

List of usage examples for org.json JSONObject has

Introduction

In this page you can find the example usage for org.json JSONObject has.

Prototype

public boolean has(String key) 

Source Link

Document

Determine if the JSONObject contains a specific key.

Usage

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Parses multi notification objects//from   w  ww.  j  a  va 2s.  c  om
 * 
 * @param notificationsJson
 * @return
 * @throws SpikaException 
 * @throws IOException 
 * @throws ClientProtocolException 
 */
public static List<Notification> parseMultiNotificationObjects(JSONArray notificationsAry)
        throws ClientProtocolException, IOException, SpikaException {

    List<Notification> notifications = new ArrayList<Notification>();

    for (int i = 0; i < notificationsAry.length(); i++) {

        try {
            JSONObject notificationJson = (JSONObject) notificationsAry.get(i);
            Notification notification = new Notification();
            notification = sGsonExpose.fromJson(notificationJson.toString(), Notification.class);

            if (notificationJson.has(Const.MESSAGES)) {
                JSONArray messagesAry = notificationJson.getJSONArray(Const.MESSAGES);
                notification.setMessages(
                        parseMultiNotificationMessageObjects(messagesAry, notification.getTargetId()));
            }

            notifications.add(notification);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return notifications;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Parse user JSON objects from get user contacts call
 * /*from   ww w  .  j a  v a2s  .  c o  m*/
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<User> parseUserContacts(JSONObject json) throws JSONException {

    List<User> users = null;

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(json));
            return null;
        }

        users = new ArrayList<User>();

        // Get the element that holds the users ( JSONArray )
        JSONArray rows = json.getJSONArray(Const.ROWS);

        for (int i = 0; i < rows.length(); i++) {

            JSONObject row = rows.getJSONObject(i);
            if (!row.isNull(Const.DOC)) {
                JSONObject userJson = row.getJSONObject(Const.DOC);

                User user = new User();

                user = sGsonExpose.fromJson(userJson.toString(), User.class);

                if (userJson.has(Const.FAVORITE_GROUPS)) {
                    JSONArray favorite_groups = userJson.getJSONArray(Const.FAVORITE_GROUPS);

                    List<String> groups = new ArrayList<String>();

                    for (int z = 0; z < favorite_groups.length(); z++) {
                        groups.add(favorite_groups.getString(z));
                    }

                    user.setGroupIds(groups);
                }

                if (userJson.has(Const.CONTACTS)) {
                    JSONArray contacts = userJson.getJSONArray(Const.CONTACTS);

                    List<String> contactsIds = new ArrayList<String>();

                    for (int j = 0; j < contacts.length(); j++) {
                        contactsIds.add(contacts.getString(j));
                    }
                    user.setContactIds(contactsIds);
                }

                users.add(user);
            }
        }
    }

    return users;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Parse comment JSON objects from get message comments
 * /*from w  ww.ja  v a2s .  co  m*/
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<Comment> parseMessageComments(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>();

        // Get the element that holds the users ( JSONArray )
        JSONArray rows = json.getJSONArray(Const.ROWS);

        for (int i = 0; i < rows.length(); i++) {

            JSONObject row = rows.getJSONObject(i);
            if (!row.isNull(Const.DOC)) {
                JSONObject commentJson = row.getJSONObject(Const.DOC);

                Comment comment = new Comment();
                comment = sGsonExpose.fromJson(commentJson.toString(), Comment.class);
                comments.add(comment);
            }
        }
    }

    return comments;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Create user response object/* w  w  w  .  j  a  v  a 2  s  . c  o  m*/
 * 
 * @param json
 * @return
 * @throws JSONException 
 */
public static String createUser(JSONObject json) throws JSONException {

    boolean ok = false;
    String id = null;

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(json));
            return null;
        }

        ok = json.getBoolean(Const.OK);
        id = json.getString(Const.ID);
    }

    if (!ok) {
        Logger.error(TAG + "createUser", "error in creating user");
    }

    return id;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Update user response object, the Const.REV value is important in order to
 * continue using the application/*www. j  a  v  a 2 s .  c o  m*/
 * 
 * If you are updating contacts or favorites on of them should be null
 * 
 * @param json
 * @return
 * @throws JSONException 
 */
public static boolean updateUser(JSONObject json, List<String> contactsIds, List<String> groupsIds)
        throws JSONException {

    String rev = "";

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(false, true, isInvalidToken(json));
            return false;
        }

        rev = json.getString(Const._REV);

        UsersManagement.getLoginUser().setRev(rev);

        if (null != contactsIds) {
            UsersManagement.getLoginUser().setContactIds(contactsIds);
        }

        if (null != groupsIds) {
            UsersManagement.getLoginUser().setGroupIds(groupsIds);
        }

        return true;
    }

    return false;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * JSON response from creating a group//from   w  w w  .  j  a v  a 2 s. c om
 * 
 * @param json
 * @return
 * @throws JSONException 
 */
public static String createGroup(JSONObject json) throws JSONException {

    boolean ok = false;
    String id = null;

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(json));
            return null;
        }

        ok = json.getInt(Const.OK) == 1 ? true : false;
        id = json.getString(Const.ID);
    }

    if (!ok) {
        Logger.error(TAG + "createGroup", "error in creating a group");
        return null;
    }

    return id;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * JSON response from deleting a group//from w ww . j a  v a2s . co m
 * 
 * @param json
 * @return
 * @throws JSONException 
 */
public static boolean deleteGroup(JSONObject json) throws JSONException {

    boolean ok = false;

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(false, false, isInvalidToken(json));
            return false;
        }

        ok = json.getBoolean(Const.OK);
    }

    return ok;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

public static String findAvatarFileId(JSONObject json) throws JSONException {
    String avatarFileId = null;/*from w  w  w.  j av  a  2  s.co m*/

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(json));
            return null;
        }

        JSONArray rows = json.getJSONArray(Const.ROWS);

        for (int i = 0; i < rows.length(); i++) {
            JSONObject row = rows.getJSONObject(i);
            avatarFileId = row.getString(Const.VALUE);
        }
    }

    return avatarFileId;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * JSON response from deleting a user group
 * //  w ww .ja v a 2 s.  c  om
 * @param json
 * @return
 * @throws JSONException 
 */
public static boolean deleteUserGroup(JSONObject json) throws JSONException {

    boolean ok = false;

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(false, false, isInvalidToken(json));
            return false;
        }

        ok = json.getBoolean(Const.OK);
    }

    return ok;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * JSON response from creating a user group
 * /*www  . ja va  2s .  c o m*/
 * @param json
 * @return
 * @throws JSONException 
 */
public static String createUserGroup(JSONObject json) throws JSONException {

    boolean ok = false;
    String id = null;

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(json));
            return null;
        }

        ok = json.getBoolean(Const.OK);
        id = json.getString(Const.ID);
    }

    if (!ok) {
        Logger.error(TAG + "createUserGroup", "error in creating a group");
        return null;
    }

    return id;
}