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

/**
 * JSON response from creating a comment
 * //from   w w w .  j a v  a2s .  co m
 * @param json
 * @return
 * @throws JSONException 
 */
public static String createComment(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 + "createComment", "error in creating comment");
        return null;
    }

    return id;
}

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

/**
 * JSON response from updating a group you own
 * /*ww w .j a v  a 2  s .  c om*/
 * @param json
 * @return
 * @throws JSONException 
 */
public static boolean updateGroup(JSONObject json) throws JSONException {

    boolean ok = false;

    if (json != null) {

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

        ok = json.getInt(Const.OK) == 1 ? true : false;

        /* Important */
        UsersManagement.getToGroup().setRev(json.getString(Const.REV));
    }

    if (!ok) {
        Logger.error(TAG + "updateGroup", "error in updating a group");
    }

    return ok;
}

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

/**
 * Parse single JSON object of type Group
 * /*from  w  ww  .ja v  a 2 s.c om*/
 * @param json
 * @return
 * @throws JSONException 
 */
public static Group parseSingleGroupObject(JSONObject json) throws JSONException {

    Group group = null;

    if (json != null) {

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

        JSONArray rows = json.getJSONArray(Const.ROWS);
        JSONObject row = rows.getJSONObject(0);

        JSONObject groupJson = row.getJSONObject(Const.VALUE);
        group = sGsonExpose.fromJson(groupJson.toString(), Group.class);
    }

    return group;
}

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

/**
* Parse single JSON object of type Group
* 
* @param json//from w  ww  .  j  ava 2  s  .  co m
* @return
*/
public static Group parseSingleGroupObjectWithoutRowParam(JSONObject json) {

    Group group = null;

    if (json != null) {

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

        if (json.length() == 0) {
            return null;
        }

        if (json.has(Const.NAME)) {
            group = sGsonExpose.fromJson(json.toString(), Group.class);
        }
    }

    return group;
}

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

/**
 * Parse multi JSON objects of type Group
 * /*w w  w. ja v a  2 s .  co  m*/
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<Group> parseMultiGroupObjects(JSONObject json) throws JSONException {

    List<Group> groups = null;

    if (json != null) {

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

        groups = new ArrayList<Group>();

        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 (!key.equals(Const.NULL)) {

                JSONObject groupJson = row.getJSONObject(Const.VALUE);

                Group group = sGsonExpose.fromJson(groupJson.toString(), Group.class);

                groups.add(group);
            }
        }
    }

    return groups;
}

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

/**
 * Parse favorite groups JSON objects//from ww  w . j  a  v a  2 s.  c om
 * 
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<Group> parseFavoriteGroups(JSONObject json) throws JSONException {

    List<Group> groups = null;

    if (json != null) {

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

        groups = new ArrayList<Group>();

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

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

            JSONObject row = rows.getJSONObject(i);

            JSONObject groupJson = row.getJSONObject(Const.DOC);

            String type = groupJson.getString(Const.TYPE);
            if (!type.equals(Const.GROUP)) {
                continue;
            }

            Group group = sGsonExpose.fromJson(groupJson.toString(), Group.class);

            groups.add(group);
        }
    }

    return groups;
}

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

/**
 * Parse multi JSON objects of type UserGroup
 * // w  w w  .j av a 2  s.  c om
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<UserGroup> parseMultiUserGroupObjects(JSONObject json) throws JSONException {

    List<UserGroup> usersGroup = null;

    if (json != null) {

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

        usersGroup = new ArrayList<UserGroup>();

        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 (!key.equals(Const.NULL)) {

                JSONObject userGroupJson = row.getJSONObject(Const.VALUE);

                UserGroup userGroup = sGsonExpose.fromJson(userGroupJson.toString(), UserGroup.class);
                usersGroup.add(userGroup);
            }
        }

    }

    return usersGroup;
}

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

/**
 * Parse multi JSON objects of type GroupCategory
 * //from   www. j a  v  a 2  s  .co  m
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<GroupCategory> parseMultiGroupCategoryObjects(JSONObject json) throws JSONException {
    List<GroupCategory> groupCategories = null;

    if (json != null) {

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

        groupCategories = new ArrayList<GroupCategory>();

        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 (!key.equals(Const.NULL)) {

                JSONObject groupCategoryJson = row.getJSONObject(Const.VALUE);

                GroupCategory groupCategory = sGsonExpose.fromJson(groupCategoryJson.toString(),
                        GroupCategory.class);

                if (groupCategoryJson.has(Const.ATTACHMENTS)) {
                    List<Attachment> attachments = new ArrayList<Attachment>();

                    JSONObject json_attachments = groupCategoryJson.getJSONObject(Const.ATTACHMENTS);

                    @SuppressWarnings("unchecked")
                    Iterator<String> keys = json_attachments.keys();
                    while (keys.hasNext()) {
                        String attachmentKey = keys.next();
                        try {
                            JSONObject json_attachment = json_attachments.getJSONObject(attachmentKey);
                            Attachment attachment = sGsonExpose.fromJson(json_attachment.toString(),
                                    Attachment.class);
                            attachment.setName(attachmentKey);
                            attachments.add(attachment);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    groupCategory.setAttachments(attachments);
                }

                groupCategories.add(groupCategory);
            }
        }
    }

    return groupCategories;
}

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

/**
 * //from ww w  . j  a v  a 2  s  .com
 * @param json
 * @return
 * @throws JSONException 
 */
public static int getCommentCount(JSONObject json) throws JSONException {

    int count = 0;

    if (json != null) {

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

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

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

            JSONObject row = rows.getJSONObject(i);
            count = row.getInt(Const.VALUE);

        }
    }

    return count;
}

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

/**
 * Find a single Message object/*from  www. ja va  2 s.c  o m*/
 * 
 * @param json
 * @return
 * @throws SpikaException 
 * @throws JSONException 
 * @throws IOException 
 * @throws ClientProtocolException 
 */
public static Message findMessage(JSONObject json)
        throws ClientProtocolException, IOException, JSONException, SpikaException {

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

    return parseMessageObject(json, false, false, false);
}