Example usage for org.json JSONObject optInt

List of usage examples for org.json JSONObject optInt

Introduction

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

Prototype

public int optInt(String key) 

Source Link

Document

Get an optional int value associated with a key, or zero if there is no such key or if the value is not a number.

Usage

From source file:com.github.mhendred.face4j.model.Face.java

public Face(JSONObject jObj) throws JSONException {
    tid = jObj.getString("tid");
    label = jObj.optString("label");

    confirmed = jObj.getBoolean("confirmed");
    manual = jObj.getBoolean("manual");

    width = (float) jObj.getDouble("width");
    height = (float) jObj.getDouble("height");

    yaw = (float) jObj.getDouble("yaw");
    roll = (float) jObj.getDouble("roll");
    pitch = (float) jObj.getDouble("pitch");

    threshold = jObj.optInt("threshold");

    center = fromJson(jObj.optJSONObject("center"));

    leftEye = fromJson(jObj.optJSONObject("eye_left"));
    rightEye = fromJson(jObj.optJSONObject("eye_right"));

    leftEar = fromJson(jObj.optJSONObject("ear_left"));
    rightEar = fromJson(jObj.optJSONObject("ear_right"));

    chin = fromJson(jObj.optJSONObject("chin"));

    mouthCenter = fromJson(jObj.optJSONObject("mouth_center"));
    mouthRight = fromJson(jObj.optJSONObject("mouth_right"));
    mouthLeft = fromJson(jObj.optJSONObject("mouth_left"));

    nose = fromJson(jObj.optJSONObject("nose"));

    guesses = Guess.fromJsonArray(jObj.optJSONArray("uids"));

    // Attributes
    jObj = jObj.getJSONObject("attributes");

    if (jObj.has("smiling"))
        smiling = jObj.getJSONObject("smiling").getBoolean("value");

    if (jObj.has("glasses"))
        glasses = jObj.getJSONObject("glasses").getBoolean("value");

    if (jObj.has("gender"))
        gender = Gender.valueOf(jObj.getJSONObject("gender").getString("value"));

    if (jObj.has("mood"))
        mood = jObj.getJSONObject("mood").getString("value");

    if (jObj.has("lips"))
        lips = jObj.getJSONObject("lips").getString("value");

    if (jObj.has("age-est"))
        ageEst = jObj.getJSONObject("age-est").getInt("vaule");

    if (jObj.has("age-min"))
        ageMin = jObj.getJSONObject("age-min").getInt("vaule");

    if (jObj.has("age-max"))
        ageMax = jObj.getJSONObject("age-max").getInt("vaule");

    faceConfidence = jObj.getJSONObject("face").getInt("confidence");

    faceRect = new Rect(center, width, height);
}

From source file:org.mariotaku.twidere.extension.mediauploader.util.ParseUtils.java

public static Bundle jsonToBundle(final String string) {
    final Bundle bundle = new Bundle();
    if (string != null) {
        try {// w  w w  .j a v  a2s  .com
            final JSONObject json = new JSONObject(string);
            final Iterator<?> it = json.keys();
            while (it.hasNext()) {
                final Object key_obj = it.next();
                if (key_obj == null) {
                    continue;
                }
                final String key = key_obj.toString();
                final Object value = json.get(key);
                if (value instanceof Boolean) {
                    bundle.putBoolean(key, json.optBoolean(key));
                } else if (value instanceof Integer) {
                    // Simple workaround for account_id
                    if (shouldPutLong(key)) {
                        bundle.putLong(key, json.optLong(key));
                    } else {
                        bundle.putInt(key, json.optInt(key));
                    }
                } else if (value instanceof Long) {
                    bundle.putLong(key, json.optLong(key));
                } else if (value instanceof String) {
                    bundle.putString(key, json.optString(key));
                } else {
                    Log.w(LOGTAG,
                            "Unknown type " + value.getClass().getSimpleName() + " in arguments key " + key);
                }
            }
        } catch (final JSONException e) {
            e.printStackTrace();
        } catch (final ClassCastException e) {
            e.printStackTrace();
        }
    }
    return bundle;
}

From source file:com.alchemiasoft.common.model.Book.java

public static Book oneFrom(@NonNull JSONObject json) {
    final Book book = new Book();
    book.mServerId = json.optString("serverId");
    book.mTitle = json.optString("title");
    book.mAuthor = json.optString("author");
    book.mSource = json.optString("source");
    book.mDescription = json.optString("description");
    book.mPages = json.optInt("pages");
    return book;/* w w w .  ja  va  2s.  co m*/
}

From source file:com.xorcode.andtweet.TwitterUser.java

/**
 * Verify the user's credentials. Returns true if authentication was
 * successful//from  w  w w .  j  a  v  a 2  s.co m
 * 
 * @see CredentialsVerified
 * @param reVerify Verify even if it was verified already
 * @return boolean
 * @throws ConnectionException
 * @throws ConnectionUnavailableException
 * @throws ConnectionAuthenticationException
 * @throws SocketTimeoutException
 * @throws ConnectionCredentialsOfOtherUserException
 */
public boolean verifyCredentials(boolean reVerify) throws ConnectionException, ConnectionUnavailableException,
        ConnectionAuthenticationException, SocketTimeoutException, ConnectionCredentialsOfOtherUserException {
    boolean ok = false;
    if (!reVerify) {
        if (getCredentialsVerified() == CredentialsVerified.SUCCEEDED) {
            ok = true;
        }
    }
    if (!ok) {
        JSONObject jso = null;
        try {
            jso = getConnection().verifyCredentials();
            ok = (jso != null);
        } finally {
            String newName = null;
            boolean credentialsOfOtherUser = false;
            boolean errorSettingUsername = false;
            if (ok) {
                if (jso.optInt("id") < 1) {
                    ok = false;
                }
            }
            if (ok) {
                newName = Connection.getScreenName(jso);
                ok = isUsernameValid(newName);
            }

            if (ok) {
                if (getUsername().length() > 0 && getUsername().compareTo(newName) != 0) {
                    // Credentials belong to other User ??
                    ok = false;
                    credentialsOfOtherUser = true;
                }
            }
            if (ok) {
                setCredentialsVerified(CredentialsVerified.SUCCEEDED);
            }
            if (ok && isTemporal()) {
                // Now we know the name of this User!
                ok = setUsernameAuthenticated(newName);
                if (!ok) {
                    errorSettingUsername = true;
                }
            }
            if (!ok) {
                clearAuthInformation();
                setCredentialsVerified(CredentialsVerified.FAILED);
            }

            if (credentialsOfOtherUser) {
                Log.e(TAG, MyPreferences.getContext().getText(R.string.error_credentials_of_other_user) + ": "
                        + newName);
                throw (new ConnectionCredentialsOfOtherUserException(newName));
            }
            if (errorSettingUsername) {
                String msg = MyPreferences.getContext().getText(R.string.error_set_username) + newName;
                Log.e(TAG, msg);
                throw (new ConnectionAuthenticationException(msg));
            }
        }
    }
    return ok;
}

From source file:com.vk.sdkweb.api.model.VKApiVideo.java

/**
 * Fills a Video instance from JSONObject.
 *//*from w  w  w .ja v  a2s  .co m*/
public VKApiVideo parse(JSONObject from) {
    id = from.optInt("id");
    owner_id = from.optInt("owner_id");
    title = from.optString("title");
    description = from.optString("description");
    duration = from.optInt("duration");
    link = from.optString("link");
    date = from.optLong("date");
    views = from.optInt("views");
    comments = from.optInt("comments");
    player = from.optString("player");
    access_key = from.optString("access_key");
    album_id = from.optInt("album_id");

    JSONObject likes = from.optJSONObject("likes");
    if (likes != null) {
        this.likes = likes.optInt("count");
        user_likes = parseBoolean(likes, "user_likes");
    }
    can_comment = parseBoolean(from, "can_comment");
    can_repost = parseBoolean(from, "can_repost");
    repeat = parseBoolean(from, "repeat");

    privacy_view = VKPrivacy.parsePrivacy(from.optJSONObject("privacy_view"));
    privacy_comment = VKPrivacy.parsePrivacy(from.optJSONObject("privacy_comment"));

    JSONObject files = from.optJSONObject("files");
    if (files != null) {
        mp4_240 = files.optString("mp4_240");
        mp4_360 = files.optString("mp4_360");
        mp4_480 = files.optString("mp4_480");
        mp4_720 = files.optString("mp4_720");
        external = files.optString("external");
    }

    photo_130 = from.optString("photo_130");
    if (!TextUtils.isEmpty(photo_130)) {
        photo.add(VKApiPhotoSize.create(photo_130, 130));
    }

    photo_320 = from.optString("photo_320");
    if (!TextUtils.isEmpty(photo_320)) {
        photo.add(VKApiPhotoSize.create(photo_320, 320));
    }

    photo_640 = from.optString("photo_640");
    if (!TextUtils.isEmpty(photo_640)) {
        photo.add(VKApiPhotoSize.create(photo_640, 640));
    }
    return this;
}

From source file:org.eclipse.flux.client.CallbackIDAwareMessageHandler.java

@Override
public boolean canHandle(String messageType, JSONObject message) {
    return super.canHandle(messageType, message) && message.has("callback_id")
            && message.optInt("callback_id") == this.expectedCallbackID;
}

From source file:org.catnut.fragment.UserTimelineFragment.java

private void loadFromCloud(long max_id) {
    mSwipeRefreshLayout.setRefreshing(true);
    CatnutAPI api = TweetAPI.userTimeline(mScreenName, 0, max_id, getFetchSize(), 0, 0, 0, 0);
    mRequestQueue.add(new CatnutRequest(getActivity(), api,
            new StatusProcessor.TimelineProcessor(Status.OTHERS, false), new Response.Listener<JSONObject>() {
                @Override/*from w w w.ja  v a  2  s. co  m*/
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "load more from cloud done...");
                    mTotal = response.optInt(TOTAL_NUMBER);
                    int newSize = response.optJSONArray(Status.MULTIPLE).length() + mAdapter.getCount();
                    Bundle args = new Bundle();
                    args.putInt(TAG, newSize);
                    getLoaderManager().restartLoader(0, args, UserTimelineFragment.this);
                }
            }, errorListener)).setTag(TAG);
}

From source file:org.catnut.fragment.UserTimelineFragment.java

@Override
protected void refresh() {
    // ???//from   w w w  .j a v  a  2  s  . c om
    if (!isNetworkAvailable()) {
        Toast.makeText(getActivity(), getString(R.string.network_unavailable), Toast.LENGTH_SHORT).show();
        initFromLocal();
        return;
    }
    // refresh!
    final int size = getFetchSize();
    (new Thread(new Runnable() {
        @Override
        public void run() {
            // ??????(?-)???Orz...
            String query = CatnutUtils.buildQuery(new String[] { BaseColumns._ID }, mSelection, Status.TABLE,
                    null, BaseColumns._ID + " desc", size + ", 1" // limit x, y
            );
            Cursor cursor = getActivity().getContentResolver().query(CatnutProvider.parse(Status.MULTIPLE),
                    null, query, null, null);
            // the cursor never null?
            final long since_id;
            if (cursor.moveToNext()) {
                since_id = cursor.getLong(0);
            } else {
                since_id = 0;
            }
            cursor.close();
            final CatnutAPI api = TweetAPI.userTimeline(mScreenName, since_id, 0, getFetchSize(), 0, 0, 0, 0);
            // refresh...
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mRequestQueue.add(new CatnutRequest(getActivity(), api,
                            new StatusProcessor.TimelineProcessor(Status.OTHERS, true),
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {
                                    Log.d(TAG, "refresh done...");
                                    mTotal = response.optInt(TOTAL_NUMBER);
                                    // ???
                                    JSONArray jsonArray = response.optJSONArray(Status.MULTIPLE);
                                    int newSize = jsonArray.length(); // ...
                                    Bundle args = new Bundle();
                                    args.putInt(TAG, newSize);
                                    getLoaderManager().restartLoader(0, args, UserTimelineFragment.this);
                                }
                            }, errorListener)).setTag(TAG);
                }
            });
        }
    })).start();
}

From source file:com.vk.sdkweb.api.model.VKApiCountry.java

/**
 * Fills a Country instance from JSONObject.
 *///from  w w  w .  j  ava  2  s .  c o m
public VKApiCountry parse(JSONObject from) {
    id = from.optInt("id");
    title = from.optString("title");
    return this;
}

From source file:org.catnut.metadata.Status.java

@Override
public ContentValues convert(JSONObject json) {
    ContentValues tweet = new ContentValues();
    tweet.put(BaseColumns._ID, json.optLong(Constants.ID));
    tweet.put(created_at, json.optString(created_at));
    // ?jsonsql???
    tweet.put(columnText, json.optString(text));
    tweet.put(source, json.optString(source));
    tweet.put(favorited, json.optBoolean(favorited));
    tweet.put(truncated, json.optBoolean(truncated));
    // ??????/*w w  w.  java2  s  .com*/
    JSONArray thumbs = json.optJSONArray(pic_urls);
    if (thumbs != null) {
        // json
        tweet.put(pic_urls, thumbs.toString());
    }
    tweet.put(thumbnail_pic, json.optString(thumbnail_pic));
    tweet.put(bmiddle_pic, json.optString(bmiddle_pic));
    tweet.put(original_pic, json.optString(original_pic));
    // ???
    if (json.has(retweeted_status)) {
        tweet.put(retweeted_status, json.optJSONObject(retweeted_status).toString());
    }
    // 
    if (json.has(User.SINGLE)) {
        tweet.put(uid, json.optJSONObject(User.SINGLE).optLong(Constants.ID));
    } else if (json.has(uid)) {
        tweet.put(uid, json.optLong(uid));
    }
    tweet.put(reposts_count, json.optInt(reposts_count));
    tweet.put(comments_count, json.optInt(comments_count));
    tweet.put(attitudes_count, json.optInt(attitudes_count));
    return tweet;
}