Example usage for org.json JSONObject getJSONObject

List of usage examples for org.json JSONObject getJSONObject

Introduction

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

Prototype

public JSONObject getJSONObject(String key) throws JSONException 

Source Link

Document

Get the JSONObject value associated with a key.

Usage

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

/**
 * Parse a single user JSON object//  w w  w .java  2 s . c o m
 * 
 * @param json
 * @return
 * @throws JSONException
 */
public static User parseSingleUserObject(JSONObject json) throws JSONException {
    User user = null;
    ArrayList<String> contactsIds = new ArrayList<String>();

    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 userJson = row.getJSONObject(Const.VALUE);

        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 i = 0; i < favorite_groups.length(); i++) {
                groups.add(favorite_groups.getString(i));
            }

            user.setGroupIds(groups);
        }

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

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

            user.setContactIds(contactsIds);
        }
    }

    return user;
}

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

/**
 * Parse multi JSON objects of type user
 * /*www .j a  v a 2 s .  co m*/
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<User> parseMultiUserObjects(JSONObject json) throws JSONException {

    List<User> users = null;
    ArrayList<String> contactsIds = new ArrayList<String>();

    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);
            JSONObject userJson = row.getJSONObject(Const.VALUE);

            User user = new User();

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

            if (userJson.has(Const.CONTACTS)) {

                JSONArray contacts = userJson.getJSONArray(Const.CONTACTS);

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

                user.setContactIds(contactsIds);
            }

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

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

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

                user.setGroupIds(groups);
            }

            users.add(user);
        }
    }

    return users;
}

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

/**
 * Parses a single activity summary JSON object
 * //from  w  w  w  .ja v  a  2  s  .com
 * @param json
 * @return
 * @throws JSONException
 * @throws SpikaException 
 * @throws IOException 
 * @throws ClientProtocolException 
 */
public static ActivitySummary parseSingleActivitySummaryObject(JSONObject json)
        throws JSONException, ClientProtocolException, IOException, SpikaException {

    ActivitySummary activitySummary = null;

    if (json != null) {

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

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

        if (rows.length() > 0) {
            JSONObject row = rows.getJSONObject(0);
            JSONObject activitySummaryJson = row.getJSONObject(Const.VALUE);

            activitySummary = new ActivitySummary();
            activitySummary = sGsonExpose.fromJson(activitySummaryJson.toString(), ActivitySummary.class);

            if (activitySummaryJson.has(Const.RECENT_ACTIVITY)) {
                JSONObject recentActivityListJson = activitySummaryJson.getJSONObject(Const.RECENT_ACTIVITY);
                List<RecentActivity> recentActivityList = CouchDBHelper
                        .parseMultiRecentActivityObjects(recentActivityListJson);
                activitySummary.setRecentActivityList(recentActivityList);
            }
        }
    }

    return activitySummary;
}

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

/**
 * Parses multi RecentActivity JSON Objects
 * /*from  w  w w .  ja v a 2 s . c om*/
 * @param recentActivityListJson
 * @return
 * @throws SpikaException 
 * @throws IOException 
 * @throws ClientProtocolException 
 */
public static List<RecentActivity> parseMultiRecentActivityObjects(JSONObject recentActivityListJson)
        throws ClientProtocolException, IOException, SpikaException {

    List<RecentActivity> recentActivityList = new ArrayList<RecentActivity>();

    @SuppressWarnings("unchecked")
    Iterator<String> iterator = recentActivityListJson.keys();
    while (iterator.hasNext()) {
        String key = iterator.next();
        try {
            JSONObject recentActivityJson = recentActivityListJson.getJSONObject(key);
            RecentActivity recentActivity = new RecentActivity();
            recentActivity = sGsonExpose.fromJson(recentActivityJson.toString(), RecentActivity.class);

            if (recentActivityJson.has(Const.NOTIFICATIONS)) {
                JSONArray notificationsJson = recentActivityJson.getJSONArray(Const.NOTIFICATIONS);
                recentActivity.set_notifications(parseMultiNotificationObjects(notificationsJson));
            }
            recentActivityList.add(recentActivity);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return recentActivityList;
}

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

/**
 * Parse user JSON objects from get user contacts call
 * //from  w  ww.  ja v  a  2 s  .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
 * //  www .  j  a  v a2  s .c  o  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

/**
 * Parse single JSON object of type Group
 * /*from ww  w  .  j a  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 multi JSON objects of type Group
 * //from ww w.  j  a va2s  .  c om
 * @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
 * //from ww w .java  2s .  co m
 * @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;
}