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:org.collectionspace.chain.csp.persistence.services.user.UserStorage.java

private JSONObject correctPassword(JSONObject in) throws JSONException, UnderlyingStorageException {
    try {//from  www .  j a v  a 2  s  .  c  o m
        if (in.has("password")) {
            String password = in.getString("password");
            in.remove("password");
            password = new String(Base64.encodeBase64(password.getBytes("UTF-8")), "UTF-8");
            while (password.endsWith("\n") || password.endsWith("\r"))
                password = password.substring(0, password.length() - 1);
            in.put("password", password);
        }
        return in;
    } catch (UnsupportedEncodingException e) {
        throw new UnderlyingStorageException("Error generating Base 64", e);
    }
}

From source file:org.collectionspace.chain.csp.persistence.services.user.UserStorage.java

private JSONObject correctScreenName(JSONObject in) throws JSONException, UnderlyingStorageException {
    if (in.has("userName") && !in.has("screenName")) {
        String username = in.getString("userName");
        in.remove("userName");
        in.put("screenName", username);
    }//from  w w  w  .  j a  v a2 s.  c  om
    return in;
}

From source file:org.collectionspace.chain.csp.persistence.services.user.UserStorage.java

private JSONObject correctUserId(JSONObject in) throws JSONException, UnderlyingStorageException {
    if (!in.has("userId")) {
        String userId = in.getString("email");
        in.remove("userId");
        in.put("userId", userId);
    }/*from w w  w  .  j a va  2s.c  om*/
    return in;
}

From source file:com.adamkruger.myipaddressinfo.IPAddressInfoFragment.java

private static String safeGetJSONString(JSONObject json, String key) throws JSONException {
    String jsonString = "";
    if (json.has(key)) {
        jsonString = json.getString(key);
    }/*w w  w . j  a  va  2 s . co m*/
    return jsonString;
}

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

/**
 * Parse a single user JSON object//from   w ww  .jav  a  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 a single user JSON object//  ww  w.  j a v a 2 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
 * //from w  w  w.  j a v  a2  s.c  om
 * @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
 * /*from   w w  w .j a va 2s  . 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 ww w. j a va2  s  .  c o  m*/
 * @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  . j a v a  2 s .  com*/
 * @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;
}