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 multi JSON objects of type Group
 * /*from   w ww . j  a  v a  2s  .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 va 2  s.c o  m*/
 * 
 * @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  w w  w. j av a2 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
 * //  w ww.  j a  v  a2 s  .c  o  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

public static List<Member> parseMemberObjects(JSONObject jsonData) throws JSONException {
    // Get total member
    int totalMember = jsonData.getInt("count");
    Member.setTotalMember(totalMember);

    // Get list member
    List<Member> listMembers = null;
    JSONArray jsonArray = jsonData.getJSONArray("users");
    if (jsonArray != null) {
        listMembers = new ArrayList<Member>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject row = jsonArray.getJSONObject(i);
            try {
                String id = row.getString("_id");
                String name = row.getString("name");
                String image = row.getString("avatar_thumb_file_id");
                String online = row.getString("online_status");
                Member member = new Member(id, name, image, online);
                listMembers.add(member);
            } catch (JSONException e) {
            }/*  ww  w.j  a va2s.  c om*/
        }
    }

    return listMembers;
}

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

/**
 * /*w  w  w. j a  va 2  s  .co m*/
 * @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 all messages for current user// ww  w. jav  a2 s  .co  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 comments Json/*  w ww  .  j  a  v a 2s  .c  o  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/*w  w w.j av  a 2 s  .  c om*/
 * 
 * @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  ww  . j a v a 2 s.  com*/
 * 
 * @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;
}