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.melniqw.instagramsdk.UserInfo.java

public static UserInfo fromJSON(JSONObject o) throws JSONException {
    if (o == null)
        return null;
    UserInfo userInfo = new UserInfo();
    userInfo.user = User.fromJSON(o);/* w  w w. j a  va  2  s .c  o  m*/
    userInfo.bio = o.optString("bio");
    userInfo.website = o.optString("website");
    JSONObject countsJSON = o.optJSONObject("counts");
    userInfo.media = countsJSON.optInt("media");
    userInfo.follows = countsJSON.optInt("follows");
    userInfo.followedBy = countsJSON.optInt("followed_by");
    return userInfo;
}

From source file:uguess.qucai.com.merchant.business.user.protocol.GetPublicKeyProcess.java

@Override
protected void onResult(JSONObject o) {
    //???/*from   ww w.  j a  v a  2 s  .c o m*/
    int value = o.optInt("result_code");
    if (value == 0) {
        /**
         * ??
         */
        JSONObject key = o.optJSONObject("body");
        if (key != null) {
            String keyString = key.optString("public_key");
            Cache.getInstance().setPublicKey(keyString);
        } else {

        }
    }
    setProcessStatus(value);
}

From source file:org.b3log.symphony.model.Liveness.java

/**
 * Calculates point of the specified liveness.
 *
 * @param liveness the specified liveness
 * @return point//from w w  w  . ja  v a2s. c  o  m
 */
public static int calcPoint(final JSONObject liveness) {
    final float activityPer = Symphonys.getFloat("activitYesterdayLivenessReward.activity.perPoint");
    final float articlePer = Symphonys.getFloat("activitYesterdayLivenessReward.article.perPoint");
    final float commentPer = Symphonys.getFloat("activitYesterdayLivenessReward.comment.perPoint");
    final float pvPer = Symphonys.getFloat("activitYesterdayLivenessReward.pv.perPoint");
    final float rewardPer = Symphonys.getFloat("activitYesterdayLivenessReward.reward.perPoint");
    final float thankPer = Symphonys.getFloat("activitYesterdayLivenessReward.thank.perPoint");
    final float votePer = Symphonys.getFloat("activitYesterdayLivenessReward.vote.perPoint");

    final int activity = liveness.optInt(Liveness.LIVENESS_ACTIVITY);
    final int article = liveness.optInt(Liveness.LIVENESS_ARTICLE);
    final int comment = liveness.optInt(Liveness.LIVENESS_COMMENT);
    int pv = liveness.optInt(Liveness.LIVENESS_PV);
    if (pv > 50) {
        pv = 50;
    }
    final int reward = liveness.optInt(Liveness.LIVENESS_REWARD);
    final int thank = liveness.optInt(Liveness.LIVENESS_THANK);
    int vote = liveness.optInt(Liveness.LIVENESS_VOTE);
    if (vote > 10) {
        vote = 10;
    }

    final int activityPoint = (int) (activity * activityPer);
    final int articlePoint = (int) (article * articlePer);
    final int commentPoint = (int) (comment * commentPer);
    final int pvPoint = (int) (pv * pvPer);
    final int rewardPoint = (int) (reward * rewardPer);
    final int thankPoint = (int) (thank * thankPer);
    final int votePoint = (int) (vote * votePer);

    int ret = activityPoint + articlePoint + commentPoint + pvPoint + rewardPoint + thankPoint + votePoint;

    final int max = Symphonys.getInt("activitYesterdayLivenessReward.maxPoint");
    if (ret > max) {
        ret = max;
    }

    return ret;
}

From source file:org.b3log.xiaov.service.TuringQueryService.java

/**
 * Chat with Turing Robot./*  ww w.  j a  va  2 s. c o  m*/
 *
 * @param userName the specified user name
 * @param msg the specified message
 * @return robot returned message, return {@code null} if not found
 */
public String chat(final String userName, String msg) {
    if (StringUtils.isBlank(msg)) {
        return null;
    }

    if (msg.startsWith(XiaoVs.QQ_BOT_NAME + " ")) {
        msg = msg.replace(XiaoVs.QQ_BOT_NAME + " ", "");
    }
    if (msg.startsWith(XiaoVs.QQ_BOT_NAME + "")) {
        msg = msg.replace(XiaoVs.QQ_BOT_NAME + "", "");
    }
    if (msg.startsWith(XiaoVs.QQ_BOT_NAME + ",")) {
        msg = msg.replace(XiaoVs.QQ_BOT_NAME + ",", "");
    }
    if (msg.startsWith(XiaoVs.QQ_BOT_NAME)) {
        msg = msg.replace(XiaoVs.QQ_BOT_NAME, "");
    }

    if (StringUtils.isBlank(userName) || StringUtils.isBlank(msg)) {
        return null;
    }

    final HTTPRequest request = new HTTPRequest();
    request.setRequestMethod(HTTPRequestMethod.POST);

    try {
        request.setURL(new URL(TURING_API));

        final String body = "key=" + URLEncoder.encode(TURING_KEY, "UTF-8") + "&info="
                + URLEncoder.encode(msg, "UTF-8") + "&userid=" + URLEncoder.encode(userName, "UTF-8");
        request.setPayload(body.getBytes("UTF-8"));

        final HTTPResponse response = URL_FETCH_SVC.fetch(request);
        final JSONObject data = new JSONObject(new String(response.getContent(), "UTF-8"));
        final int code = data.optInt("code");

        switch (code) {
        case 40001:
        case 40002:
        case 40007:
            LOGGER.log(Level.ERROR, data.optString("text"));

            return null;
        case 40004:
            return "??~";
        case 100000:
            return data.optString("text");
        case 200000:
            return data.optString("text") + " " + data.optString("url");
        case 302000:
            String ret302000 = data.optString("text") + " ";
            final JSONArray list302000 = data.optJSONArray("list");
            final StringBuilder builder302000 = new StringBuilder();
            for (int i = 0; i < list302000.length(); i++) {
                final JSONObject news = list302000.optJSONObject(i);
                builder302000.append(news.optString("article")).append(news.optString("detailurl"))
                        .append("\n\n");
            }

            return ret302000 + " " + builder302000.toString();
        case 308000:
            String ret308000 = data.optString("text") + " ";
            final JSONArray list308000 = data.optJSONArray("list");
            final StringBuilder builder308000 = new StringBuilder();
            for (int i = 0; i < list308000.length(); i++) {
                final JSONObject news = list308000.optJSONObject(i);
                builder308000.append(news.optString("name")).append(news.optString("detailurl")).append("\n\n");
            }

            return ret308000 + " " + builder308000.toString();
        default:
            LOGGER.log(Level.WARN, "Turing Robot default return [" + data.toString(4) + "]");
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Chat with Turing Robot failed", e);
    }

    return null;
}

From source file:com.github.jberkel.pay.me.model.Purchase.java

/**
 * @param itemType the item type for this purchase, cannot be null.
 * @param jsonPurchaseInfo the JSON representation of this purchase
 * @param signature the signature// w  w  w.j  av a2s  . c om
 * @throws JSONException if the purchase cannot be parsed or is invalid.
 */
public Purchase(ItemType itemType, String jsonPurchaseInfo, String signature) throws JSONException {
    if (itemType == null)
        throw new IllegalArgumentException("itemType cannot be null");
    mItemType = itemType;
    final JSONObject json = new JSONObject(jsonPurchaseInfo);

    mOrderId = json.optString(ORDER_ID);
    mPackageName = json.optString(PACKAGE_NAME);
    mSku = json.optString(PRODUCT_ID);
    mPurchaseTime = json.optLong(PURCHASE_TIME);
    mPurchaseState = json.optInt(PURCHASE_STATE);
    mDeveloperPayload = json.optString(DEVELOPER_PAYLOAD);
    mToken = json.optString(TOKEN, json.optString(PURCHASE_TOKEN));

    mOriginalJson = jsonPurchaseInfo;
    mSignature = signature;
    mState = State.fromCode(mPurchaseState);

    if (TextUtils.isEmpty(mSku)) {
        throw new JSONException("SKU is empty");
    }
}

From source file:com.findcab.driver.object.DriverInfo.java

public DriverInfo(JSONObject jObject) {

    car_license = jObject.optString("car_license");
    car_service_number = jObject.optString("car_service_number");
    car_type = jObject.optString("car_type");
    distance = jObject.optDouble("distance");
    id = jObject.optInt("id");
    lat = jObject.optDouble("lat");
    lng = jObject.optDouble("lng");
    mobile = jObject.optString("mobile");
    name = jObject.optString("name");
    password = jObject.optString("password");
    rate = jObject.optInt("rate");
    updated_at = jObject.optString("updated_at");

}

From source file:com.ymt.demo1.plates.hub.FireHubMainFragment.java

/**
 * hub??/* w w w.  j a  v a 2s.c o m*/
 */
public StringRequest hubPlateRequest() {
    return new StringRequest(BaseURLUtil.PLATE_REQUEST_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            //                Log.e("TAG", ">>>>>>>>>>>>>.s  " + s);
            try {
                JSONObject jsonObject = new JSONObject(s);
                if (jsonObject.getInt("retCode") == 0) { //?
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    int length = jsonArray.length();
                    for (int i = 0; i < length; i++) {
                        JSONObject object = jsonArray.getJSONObject(i);
                        HubPlate hubPlate = new HubPlate();
                        hubPlate.setFid(object.getInt("fid"));
                        hubPlate.setFup(object.getInt("fup"));
                        hubPlate.setLastpost(object.getString("lastpost"));
                        hubPlate.setName(object.getString("name"));
                        hubPlate.setType(object.getString("type"));
                        hubPlate.setRank(object.optInt("rank"));
                        hubPlate.setThreads(object.optInt("threads"));

                        plateList.add(hubPlate);
                    }
                    update1List();

                }
            } catch (JSONException e) {
                AppContext.toastBadJson();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            AppContext.toastBadInternet();
        }
    });

}

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

/**
 * Fills a Photo instance from JSONObject.
 *///w ww . ja  v a 2 s .  c  o  m
public VKApiPhoto parse(JSONObject from) {
    album_id = from.optInt("album_id");
    date = from.optLong("date");
    height = from.optInt("height");
    width = from.optInt("width");
    owner_id = from.optInt("owner_id");
    id = from.optInt("id");
    text = from.optString("text");
    access_key = from.optString("access_key");

    photo_75 = from.optString("photo_75");
    photo_130 = from.optString("photo_130");
    photo_604 = from.optString("photo_604");
    photo_807 = from.optString("photo_807");
    photo_1280 = from.optString("photo_1280");
    photo_2560 = from.optString("photo_2560");

    JSONObject likes = from.optJSONObject("likes");
    this.likes = ParseUtils.parseInt(likes, "count");
    this.user_likes = ParseUtils.parseBoolean(likes, "user_likes");
    comments = parseInt(from.optJSONObject("comments"), "count");
    tags = parseInt(from.optJSONObject("tags"), "count");
    can_comment = parseBoolean(from, "can_comment");

    src.setOriginalDimension(width, height);
    JSONArray photo_sizes = from.optJSONArray("sizes");
    if (photo_sizes != null) {
        src.fill(photo_sizes);
    } else {
        if (!TextUtils.isEmpty(photo_75)) {
            src.add(VKApiPhotoSize.create(photo_75, VKApiPhotoSize.S, width, height));
        }
        if (!TextUtils.isEmpty(photo_130)) {
            src.add(VKApiPhotoSize.create(photo_130, VKApiPhotoSize.M, width, height));
        }
        if (!TextUtils.isEmpty(photo_604)) {
            src.add(VKApiPhotoSize.create(photo_604, VKApiPhotoSize.X, width, height));
        }
        if (!TextUtils.isEmpty(photo_807)) {
            src.add(VKApiPhotoSize.create(photo_807, VKApiPhotoSize.Y, width, height));
        }
        if (!TextUtils.isEmpty(photo_1280)) {
            src.add(VKApiPhotoSize.create(photo_1280, VKApiPhotoSize.Z, width, height));
        }
        if (!TextUtils.isEmpty(photo_2560)) {
            src.add(VKApiPhotoSize.create(photo_2560, VKApiPhotoSize.W, width, height));
        }
        src.sort();
    }
    return this;
}

From source file:net.geco.model.iojson.PersistentStore.java

public void importRunnersData(JSONStore store, Registry registry, Factory factory) throws JSONException {
    final int I_RUNNER = 0;
    final int I_ECARD = 1;
    final int I_RESULT = 2;
    JSONArray runnersData = store.getJSONArray(K.RUNNERS_DATA);
    for (int i = 0; i < runnersData.length(); i++) {
        JSONArray runnerTuple = runnersData.getJSONArray(i);

        JSONObject c = runnerTuple.getJSONObject(I_RUNNER);
        Runner runner = factory.createRunner();
        runner.setStartId(c.getInt(K.START_ID));
        runner.setFirstname(c.getString(K.FIRST));
        runner.setLastname(c.getString(K.LAST));
        runner.setEcard(c.getString(K.ECARD));
        runner.setClub(store.retrieve(c.getInt(K.CLUB), Club.class));
        runner.setCategory(store.retrieve(c.getInt(K.CAT), Category.class));
        runner.setCourse(store.retrieve(c.getInt(K.COURSE), Course.class));
        runner.setRegisteredStarttime(new Date(c.getLong(K.START)));
        runner.setArchiveId((Integer) c.opt(K.ARK));
        runner.setRentedEcard(c.optBoolean(K.RENT));
        runner.setNC(c.optBoolean(K.NC));
        registry.addRunner(runner);//from  ww  w.  ja va 2  s  .c  o  m

        JSONObject d = runnerTuple.getJSONObject(I_ECARD);
        RunnerRaceData raceData = factory.createRunnerRaceData();
        raceData.setStarttime(new Date(d.getLong(K.START)));
        raceData.setFinishtime(new Date(d.getLong(K.FINISH)));
        raceData.setControltime(new Date(d.getLong(K.CHECK)));
        raceData.setReadtime(new Date(d.getLong(K.READ)));
        JSONArray p = d.getJSONArray(K.PUNCHES);
        Punch[] punches = new Punch[p.length() / 2];
        for (int j = 0; j < punches.length; j++) {
            punches[j] = factory.createPunch();
            punches[j].setCode(p.getInt(2 * j));
            punches[j].setTime(new Date(p.getLong(2 * j + 1)));
        }
        raceData.setPunches(punches);
        raceData.setRunner(runner);
        registry.addRunnerData(raceData);

        JSONObject r = runnerTuple.getJSONObject(I_RESULT);
        TraceData traceData = factory.createTraceData();
        traceData.setNbMPs(r.getInt(K.MPS));
        traceData.setNbExtraneous(r.optInt(K.EXTRA)); // MIGR v2.x -> v2.3
        JSONArray t = r.getJSONArray(K.TRACE);
        Trace[] trace = new Trace[t.length() / 2];
        for (int j = 0; j < trace.length; j++) {
            trace[j] = factory.createTrace(t.getString(2 * j), new Date(t.getLong(2 * j + 1)));
        }
        if (r.has(K.SECTION_DATA)) {
            SectionTraceData sectionData = (SectionTraceData) traceData;
            JSONArray sections = r.getJSONArray(K.SECTION_DATA);
            for (int j = 0; j < sections.length(); j++) {
                JSONArray section = sections.getJSONArray(j);
                sectionData.putSectionAt(store.retrieve(section.getInt(0), Section.class), section.getInt(1));
            }
        }
        JSONArray neut = r.getJSONArray(K.NEUTRALIZED);
        for (int j = 0; j < neut.length(); j++) {
            trace[neut.getInt(j)].setNeutralized(true);
        }
        traceData.setTrace(trace);
        raceData.setTraceData(traceData);

        RunnerResult result = factory.createRunnerResult();
        result.setRaceTime(r.optLong(K.RACE_TIME, TimeManager.NO_TIME_l)); // MIGR v2.x -> v2.2
        result.setResultTime(r.getLong(K.TIME));
        result.setStatus(Status.valueOf(r.getString(K.STATUS)));
        result.setTimePenalty(r.getLong(K.PENALTY));
        result.setManualTimePenalty(r.optLong(K.MANUAL_PENALTY, 0)); // MIGR v2.x -> v2.3
        raceData.setResult(result);
    }
}

From source file:com.basetechnology.s0.agentserver.field.MultiChoiceField.java

public static Field fromJson(SymbolTable symbolTable, JSONObject fieldJson) {
    String type = fieldJson.optString("type");
    if (type == null || !type.equals("multi_choice_field"))
        return null;
    String name = fieldJson.has("name") ? fieldJson.optString("name") : null;
    String label = fieldJson.has("label") ? fieldJson.optString("label") : null;
    String description = fieldJson.has("description") ? fieldJson.optString("description") : null;
    String defaultValue = fieldJson.has("default_value") ? fieldJson.optString("default_value") : null;
    List<String> choices = new ArrayList<String>();
    if (fieldJson.has("choices")) {
        JSONArray choicesJson = fieldJson.optJSONArray("choices");
        int n = choicesJson.length();
        for (int i = 0; i < n; i++)
            choices.add(choicesJson.optString(i));
    }/*  w  w w .j  a  v a 2 s  . com*/
    int nominalWidth = fieldJson.has("nominal_width") ? fieldJson.optInt("nominal_width") : 0;
    String compute = fieldJson.has("compute") ? fieldJson.optString("compute") : null;
    return new MultiChoiceField(symbolTable, name, label, description, defaultValue, choices, nominalWidth,
            compute);
}