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.basetechnology.s0.agentserver.field.ChoiceField.java

public static Field fromJson(SymbolTable symbolTable, JSONObject fieldJson) {
    String type = fieldJson.optString("type");
    if (type == null || !type.equals("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));
    }//from  w ww .ja  va 2s  .com
    int nominalWidth = fieldJson.has("nominal_width") ? fieldJson.optInt("nominal_width") : 0;
    String compute = fieldJson.has("compute") ? fieldJson.optString("compute") : null;
    return new ChoiceField(symbolTable, name, label, description, defaultValue, choices, nominalWidth, compute);
}

From source file:com.mifos.mifosxdroid.online.grouploanaccount.GroupLoanAccountFragment.java

private void inflateAmortizationSpinner(ResponseBody result) {

    final List<AmortizationType> amortizationType = new ArrayList<>();
    // you can use this array to populate your spinner
    final ArrayList<String> amortizationTypeNames = new ArrayList<String>();
    //Try to get response body
    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();
    try {//  www  .  j  a  v  a  2s .c  o m
        reader = new BufferedReader(new InputStreamReader(result.byteStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        JSONObject obj = new JSONObject(sb.toString());
        if (obj.has("amortizationTypeOptions")) {
            JSONArray amortizationTypes = obj.getJSONArray("amortizationTypeOptions");
            for (int i = 0; i < amortizationTypes.length(); i++) {
                JSONObject amortizationTypeObject = amortizationTypes.getJSONObject(i);
                AmortizationType amortization = new AmortizationType();
                amortization.setId(amortizationTypeObject.optInt("id"));
                amortization.setValue(amortizationTypeObject.optString("value"));
                amortizationType.add(amortization);
                amortizationTypeNames.add(amortizationTypeObject.optString("value"));
                amortizationTypeIdHashMap.put(amortization.getValue(), amortization.getId());
            }

        }
        String stringResult = sb.toString();
    } catch (Exception e) {
        Log.e(LOG_TAG, "", e);
    }
    final ArrayAdapter<String> amortizationTypeAdapter = new ArrayAdapter<>(getActivity(),
            layout.simple_spinner_item, amortizationTypeNames);
    amortizationTypeAdapter.setDropDownViewResource(layout.simple_spinner_dropdown_item);
    sp_amortization.setAdapter(amortizationTypeAdapter);
    sp_amortization.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            amortizationTypeId = amortizationTypeIdHashMap.get(amortizationTypeNames.get(i));
            Log.d("ammortization" + amortizationTypeNames.get(i), String.valueOf(amortizationTypeId));
            if (amortizationTypeId != -1) {

            } else {

                Toast.makeText(getActivity(), getString(R.string.error_select_fund), Toast.LENGTH_SHORT).show();

            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

From source file:com.mifos.mifosxdroid.online.grouploanaccount.GroupLoanAccountFragment.java

private void inflateInterestCalculationPeriodSpinner(ResponseBody result) {

    final List<InterestCalculationPeriodType> interestCalculationPeriodType = new ArrayList<>();
    // you can use this array to populate your spinner
    final ArrayList<String> interestCalculationPeriodTypeNames = new ArrayList<String>();
    //Try to get response body
    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();
    try {/*from  w w  w. ja  va2s  .  c om*/
        reader = new BufferedReader(new InputStreamReader(result.byteStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        JSONObject obj = new JSONObject(sb.toString());
        if (obj.has("interestCalculationPeriodTypeOptions")) {
            JSONArray interestCalculationPeriodTypes = obj.getJSONArray("interestCalculationPeriodTypeOptions");
            for (int i = 0; i < interestCalculationPeriodTypes.length(); i++) {
                JSONObject interestCalculationPeriodTypeObject = interestCalculationPeriodTypes
                        .getJSONObject(i);
                InterestCalculationPeriodType interestCalculationPeriod = new InterestCalculationPeriodType();
                interestCalculationPeriod.setId(interestCalculationPeriodTypeObject.optInt("id"));
                interestCalculationPeriod.setValue(interestCalculationPeriodTypeObject.optString("value"));
                interestCalculationPeriodType.add(interestCalculationPeriod);
                interestCalculationPeriodTypeNames.add(interestCalculationPeriodTypeObject.optString("value"));
                interestCalculationPeriodTypeIdHashMap.put(interestCalculationPeriod.getValue(),
                        interestCalculationPeriod.getId());
            }

        }
        String stringResult = sb.toString();
    } catch (Exception e) {
        Log.e(LOG_TAG, "", e);
    }
    final ArrayAdapter<String> interestCalculationPeriodTypeAdapter = new ArrayAdapter<>(getActivity(),
            layout.simple_spinner_item, interestCalculationPeriodTypeNames);

    interestCalculationPeriodTypeAdapter.setDropDownViewResource(layout.simple_spinner_dropdown_item);
    sp_interestcalculationperiod.setAdapter(interestCalculationPeriodTypeAdapter);
    sp_interestcalculationperiod.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            interestCalculationPeriodTypeId = interestCalculationPeriodTypeIdHashMap
                    .get(interestCalculationPeriodTypeNames.get(i));
            Log.d("interestCalculation " + interestCalculationPeriodTypeNames.get(i),
                    String.valueOf(interestCalculationPeriodTypeId));
            if (interestCalculationPeriodTypeId != -1) {

            } else {

                Toast.makeText(getActivity(), getString(R.string.error_select_interestCalculationPeriod),
                        Toast.LENGTH_SHORT).show();

            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

From source file:com.mifos.mifosxdroid.online.grouploanaccount.GroupLoanAccountFragment.java

private void inflatetransactionProcessingStrategySpinner(ResponseBody result) {

    final List<TransactionProcessingStrategy> transactionProcessingStrategyType = new ArrayList<>();
    // you can use this array to populate your spinner
    final ArrayList<String> transactionProcessingStrategyTypeNames = new ArrayList<String>();
    //Try to get response body
    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();
    try {/*from   w w  w.j av a 2s  .  c o  m*/
        reader = new BufferedReader(new InputStreamReader(result.byteStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        JSONObject obj = new JSONObject(sb.toString());
        if (obj.has("transactionProcessingStrategyOptions")) {
            JSONArray transactionProcessingStrategyTypes = obj
                    .getJSONArray("transactionProcessingStrategyOptions");
            for (int i = 0; i < transactionProcessingStrategyTypes.length(); i++) {
                JSONObject transactionProcessingStrategyTypeObject = transactionProcessingStrategyTypes
                        .getJSONObject(i);
                TransactionProcessingStrategy transactionProcessingStrategy = new TransactionProcessingStrategy();
                transactionProcessingStrategy.setId(transactionProcessingStrategyTypeObject.optInt("id"));
                transactionProcessingStrategy
                        .setName(transactionProcessingStrategyTypeObject.optString("name"));
                transactionProcessingStrategyType.add(transactionProcessingStrategy);
                transactionProcessingStrategyTypeNames
                        .add(transactionProcessingStrategyTypeObject.optString("name"));
                transactionProcessingStrategyTypeIdHashMap.put(transactionProcessingStrategy.getName(),
                        transactionProcessingStrategy.getId());
            }

        }
        String stringResult = sb.toString();
    } catch (Exception e) {
        Log.e(LOG_TAG, "", e);
    }
    final ArrayAdapter<String> transactionProcessingStrategyAdapter = new ArrayAdapter<>(getActivity(),
            layout.simple_spinner_item, transactionProcessingStrategyTypeNames);

    transactionProcessingStrategyAdapter.setDropDownViewResource(layout.simple_spinner_dropdown_item);

    sp_repaymentstrategy.setAdapter(transactionProcessingStrategyAdapter);
    sp_repaymentstrategy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            transactionProcessingStrategyId = transactionProcessingStrategyTypeIdHashMap
                    .get(transactionProcessingStrategyTypeNames.get(i));
            Log.d("transactionProcessing " + transactionProcessingStrategyTypeNames.get(i),
                    String.valueOf(transactionProcessingStrategyId));
            if (transactionProcessingStrategyId != -1) {

            } else {

                Toast.makeText(getActivity(), getString(R.string.error_select_transactionProcessingStrategy),
                        Toast.LENGTH_SHORT).show();

            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

From source file:com.mifos.mifosxdroid.online.grouploanaccount.GroupLoanAccountFragment.java

private void inflateFrequencyPeriodSpinner(ResponseBody result) {

    final List<TermFrequencyTypeOptions> termFrequencyType = new ArrayList<>();
    // you can use this array to populate your spinner
    final ArrayList<String> termFrequencyTypeNames = new ArrayList<String>();
    //Try to get response body
    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();
    try {//  w  ww .ja v  a2 s.  com
        reader = new BufferedReader(new InputStreamReader(result.byteStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        JSONObject obj = new JSONObject(sb.toString());
        if (obj.has("termFrequencyTypeOptions")) {
            JSONArray termFrequencyTypes = obj.getJSONArray("termFrequencyTypeOptions");
            for (int i = 0; i < termFrequencyTypes.length(); i++) {
                JSONObject termFrequencyTypeObject = termFrequencyTypes.getJSONObject(i);
                TermFrequencyTypeOptions termFrequency = new TermFrequencyTypeOptions();
                termFrequency.setId(termFrequencyTypeObject.optInt("id"));
                termFrequency.setValue(termFrequencyTypeObject.optString("value"));
                termFrequencyType.add(termFrequency);
                termFrequencyTypeNames.add(termFrequencyTypeObject.optString("value"));
                termFrequencyTypeIdHashMap.put(termFrequency.getValue(), termFrequency.getId());
            }

        }
        String stringResult = sb.toString();
    } catch (Exception e) {
        Log.e(LOG_TAG, "", e);
    }
    final ArrayAdapter<String> termFrequencyTypeAdapter = new ArrayAdapter<>(getActivity(),
            layout.simple_spinner_item, termFrequencyTypeNames);
    termFrequencyTypeAdapter.setDropDownViewResource(layout.simple_spinner_dropdown_item);
    sp_payment_periods.setAdapter(termFrequencyTypeAdapter);
    sp_payment_periods.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            loanTermFrequency = termFrequencyTypeIdHashMap.get(termFrequencyTypeNames.get(i));
            Log.d("termFrequencyTypeId" + termFrequencyTypeNames.get(i), String.valueOf(loanTermFrequency));
            if (loanTermFrequency != -1) {

            } else {

                Toast.makeText(getActivity(), getString(R.string.error_select_fund), Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

From source file:com.mifos.mifosxdroid.online.grouploanaccount.GroupLoanAccountFragment.java

@Override
public void showLoanPurposeSpinner(ResponseBody result) {

    mResponse = result;//from  w  ww.j a v  a2  s  .  c om

    inflateFrequencyPeriodSpinner(mResponse);
    inflateAmortizationSpinner(mResponse);
    inflateInterestCalculationPeriodSpinner(mResponse);
    //inflaterepaymentFrequencyTypeOptionsSpinner();
    inflatetransactionProcessingStrategySpinner(mResponse);

    final List<LoanPurposeOptions> loanPurposeOptionsType = new ArrayList<>();
    // you can use this array to populate your spinner
    final ArrayList<String> loanPurposeOptionsTypeNames = new ArrayList<String>();
    //Try to get response body
    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();
    try {
        reader = new BufferedReader(new InputStreamReader(result.byteStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        JSONObject obj = new JSONObject(sb.toString());
        if (obj.has("loanPurposeOptions")) {
            JSONArray loanPurposeOptionsTypes = obj.getJSONArray("loanPurposeOptions");
            for (int i = 0; i < loanPurposeOptionsTypes.length(); i++) {
                JSONObject loanPurposeOptionsTypesObject = loanPurposeOptionsTypes.getJSONObject(i);
                LoanPurposeOptions loanpurpose = new LoanPurposeOptions();
                loanpurpose.setId(loanPurposeOptionsTypesObject.optInt("id"));
                loanpurpose.setName(loanPurposeOptionsTypesObject.optString("name"));
                loanPurposeOptionsType.add(loanpurpose);
                loanPurposeOptionsTypeNames.add(loanPurposeOptionsTypesObject.optString("name"));
                loanPurposeNameIdHashMap.put(loanpurpose.getName(), loanpurpose.getId());
            }

        }
        String stringResult = sb.toString();
    } catch (Exception e) {
        Log.e(LOG_TAG, "", e);
    }
    final ArrayAdapter<String> loanPTypeAdapter = new ArrayAdapter<>(getActivity(), layout.simple_spinner_item,
            loanPurposeOptionsTypeNames);

    loanPTypeAdapter.setDropDownViewResource(layout.simple_spinner_dropdown_item);
    sp_loan_purpose.setAdapter(loanPTypeAdapter);
    sp_loan_purpose.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            loanPurposeId = loanPurposeNameIdHashMap.get(loanPurposeOptionsTypeNames.get(i));
            Log.d("loanpurpose" + loanPurposeOptionsTypeNames.get(i), String.valueOf(loanPurposeId));
            if (loanPurposeId != -1) {

            } else {

                Toast.makeText(getActivity(), getString(R.string.error_select_fund), Toast.LENGTH_SHORT).show();

            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

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

public static Field fromJson(SymbolTable symbolTable, JSONObject fieldJson) {
    String type = fieldJson.optString("type").toLowerCase();
    if (type == null || !type.equals("string"))
        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;
    int minLength = fieldJson.has("min_length") ? fieldJson.optInt("min_length") : 0;
    int maxLength = fieldJson.has("max_length") ? fieldJson.optInt("max_length") : 0;
    int nominalWidth = fieldJson.has("nominal_width") ? fieldJson.optInt("nominal_width") : 0;
    String validRegex = fieldJson.has("valid_regex") ? fieldJson.optString("valid_regex") : null;
    String compute = fieldJson.has("compute") ? fieldJson.optString("compute") : null;
    return new StringField(symbolTable, name, label, description, defaultValue, minLength, maxLength,
            nominalWidth, validRegex, compute);
}

From source file:com.jelly.music.player.GMusicHelpers.WebClientSongsSchema.java

@Override
public WebClientSongsSchema fromJsonObject(JSONObject jsonObject) {

    if (jsonObject != null) {
        mTotalTracks = jsonObject.optInt("totalTracks");
        mSubjectToCuration = jsonObject.optBoolean("subjectToCuration");
        mName = jsonObject.optString("name", null);
        mTotalDiscs = jsonObject.optInt("totalDiscs");
        mTitleNorm = jsonObject.optString("titleNorm", null);
        mAlbumNorm = jsonObject.optString("albumNorm", null);
        mTrack = jsonObject.optInt("track");
        mAlbumArtUrl = jsonObject.optString("albumArtUrl", null);
        mUrl = jsonObject.optString("url", null);
        mCreationDate = jsonObject.optLong("creationDate");
        mAlbumArtistNorm = jsonObject.optString("albumArtistNorm", null);
        mArtistNorm = jsonObject.optString("artistNorm", null);
        mLastPlayed = jsonObject.optLong("lastPlayed");
        mMatchedId = jsonObject.optString("matchedId", null);
        mType = jsonObject.optInt("type");
        mDisc = jsonObject.optInt("disc");
        mGenre = jsonObject.optString("genre", null);
        mBeatsPerMinute = jsonObject.optInt("beatsPerMinute");
        mAlbum = jsonObject.optString("album", null);
        mId = jsonObject.optString("id", null);
        mComposer = jsonObject.optString("composer", null);
        mTitle = jsonObject.optString("title", null);
        mAlbumArtist = jsonObject.optString("albumArtist", null);
        mYear = jsonObject.optInt("year");
        mArtist = jsonObject.optString("artist", null);
        mDurationMillis = jsonObject.optLong("durationMillis");
        mIsDeleted = jsonObject.optBoolean("deleted");
        mPlayCount = jsonObject.optInt("playCount");
        mRating = jsonObject.optString("rating", null);
        mComment = jsonObject.optString("comment", null);
        mPlaylistEntryId = jsonObject.optString("playlistEntryId");
    }//from  w w  w .  j  ava2s .  c om

    //This method returns itself to support chaining.
    return this;
}

From source file:com.example.protocol.PLAYER.java

public void fromJson(JSONObject jsonObject) throws JSONException {
    if (null == jsonObject) {
        return;//  w  ww . j a v  a2 s .com
    }
    this.id = jsonObject.optInt("id");
    this.name = jsonObject.optString("name");
    this.location = jsonObject.optString("location");
    this.followers_count = jsonObject.optInt("followers_count");
    this.draftees_conut = jsonObject.optInt("draftees_conut");
    this.likes_count = jsonObject.optInt("likes_count");
    this.likes_receiced_count = jsonObject.optInt("likes_receiced_count");
    this.rebounds_count = jsonObject.optInt("rebounds_count");
    this.rebounds_received_count = jsonObject.optInt("rebounds_received_count");
    this.url = jsonObject.optString("url");
    this.avatar_url = jsonObject.optString("avatar_url");
    this.username = jsonObject.optString("username");
    this.twitter_screen_name = jsonObject.optString("twitter_screen_name");
    this.website_url = jsonObject.optString("website_url");
    this.drafted_by_player_id = jsonObject.optInt("drafted_by_player_id");
    this.shots_count = jsonObject.optInt("shots_count");
    this.following_count = jsonObject.optInt("following_count");
    this.created_at = jsonObject.optString("created_at");
    return;
}

From source file:com.phelps.liteweibo.model.weibo.Group.java

public static Group parse(JSONObject jsonObject) {
    if (null == jsonObject) {
        return null;
    }/*  w  w w  . jav a  2  s  .c  o m*/

    Group group = new Group();
    group.user = User.parse(jsonObject.optJSONObject("user"));
    group.id = jsonObject.optString("id");
    group.idStr = jsonObject.optString("idstr");
    group.name = jsonObject.optString("name");
    group.mode = jsonObject.optString("mode");
    group.visible = jsonObject.optInt("visible");
    group.like_count = jsonObject.optInt("like_count");
    group.member_count = jsonObject.optInt("member_count");
    group.description = jsonObject.optString("description");
    group.profile_image_url = jsonObject.optString("profile_image_url");
    group.createAtTime = jsonObject.optString("create_time", "");

    JSONArray jsonArray = jsonObject.optJSONArray("tags");
    if (jsonArray != null && jsonObject.length() > 0) {
        int length = jsonArray.length();
        group.tags = new ArrayList<Tag>(length);
        for (int ix = 0; ix < length; ix++) {
            group.tags.add(Tag.parse(jsonArray.optJSONObject(ix)));
        }
    }

    return group;
}