Example usage for org.json JSONObject getJSONArray

List of usage examples for org.json JSONObject getJSONArray

Introduction

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

Prototype

public JSONArray getJSONArray(String key) throws JSONException 

Source Link

Document

Get the JSONArray value associated with a key.

Usage

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

/**
* Parse a single user JSON object/*from ww w.  j a v a2 s. com*/
* 
* @param json
* @return
* @throws JSONException
* @throws SpikaException 
*/
public static User parseSingleUserObjectWithoutRowParam(JSONObject userJson)
        throws JSONException, SpikaException {
    User user = null;
    ArrayList<String> contactsIds = new ArrayList<String>();

    if (userJson != null) {

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

        if (userJson.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(userJson));
            throw new SpikaException(ConnectionHandler.getError(userJson));
        }

        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
 * /*w w  w  . j  a  v  a 2s.c o 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

/**
 * Parse multi JSON objects of type user for search users
 * // w w w .  j  av  a  2 s. c o m
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<User> parseSearchUsersResult(JSONArray jsonArray) throws JSONException {

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

    if (jsonArray != null) {

        users = new ArrayList<User>();

        // Get the element that holds the users ( JSONArray )

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

            JSONObject userJson = jsonArray.getJSONObject(i);

            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  .j a v a  2 s  . c  om*/
 * @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
 * //w  ww  .ja  va2  s.  c o  m
 * @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

/**
 * Parses multi notification objects// w  w w . ja v  a 2  s  .  c  o m
 * 
 * @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 .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
 * //from www.  ja v  a 2 s .  c om
 * @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

public static String findAvatarFileId(JSONObject json) throws JSONException {
    String avatarFileId = null;//from   w  ww  . j a  v a 2s  . c  o  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

/**
 * Parse single JSON object of type Group
 * //w ww.  ja v a  2  s .  co m
 * @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;
}