Example usage for org.json JSONArray length

List of usage examples for org.json JSONArray length

Introduction

In this page you can find the example usage for org.json JSONArray length.

Prototype

public int length() 

Source Link

Document

Get the number of elements in the JSONArray, included nulls.

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 www . j a  v  a2  s  .  co m*/
    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.trafficspaces.api.controller.Connector.java

/****************************************************
 ***********       CRUD FUNCTIONS       *************
 ****************************************************/

public List find(Properties params) throws IOException, TrafficspacesAPIException {
    String jsonStr = sendRequest(resourcePath + "?" + toQueryString(params), "application/json");
    try {/*from w  w  w .  j a  va2 s.c o m*/
        ArrayList resourceList = null;
        if (jsonStr != null) {
            JSONArray jsonArray = new JSONArray(jsonStr);
            Object[] args = new Object[1];
            resourceList = new ArrayList();
            for (int i = 0; i < jsonArray.length(); i++) {
                args[0] = jsonArray.getJSONObject(i);

                resourceList.add(resourceConstructor.newInstance(args));
            }
        }
        return resourceList;
    } catch (Exception e) {
        throw new TrafficspacesAPIException(e);
    }
}

From source file:com.sina.weibo.sdk_lib.openapi.models.PoiList.java

public static PoiList parse(String jsonString) {
    if (TextUtils.isEmpty(jsonString)) {
        return null;
    }/*from  w  w w .j  av  a2  s. c  om*/

    PoiList poiList = new PoiList();
    try {
        JSONObject jsonObject = new JSONObject(jsonString);

        poiList.totalNumber = jsonObject.optString("total_number");
        JSONArray jsonArray = jsonObject.optJSONArray("geos");
        if (jsonArray != null && jsonArray.length() > 0) {
            int length = jsonArray.length();
            poiList.pois = new ArrayList<Poi>(length);
            for (int ix = 0; ix < length; ix++) {
                poiList.pois.add(Poi.parse(jsonArray.optJSONObject(ix)));
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return poiList;
}

From source file:project.getphrase.PhraseService.java

public ArrayList<Phrase> getPhraseS(int bmilvl, int change, int w1, int w2, int w3)
        throws MalformedURLException, IOException, JSONException {

    String url = "";
    url += Settings.BASE_PROTOCOL + Settings.PH_BASE_URL + Settings.PH_BASE_PORT + Settings.PH_BASE_PATH;
    url += "?" + Settings.PH_PARAM_BMI_LEVEL + "=" + bmilvl;
    url += "&" + Settings.PH_PARAM_CHANGE + "=" + change;
    url += "&" + Settings.PH_PARAM_WEATHER_TYPE_1 + "=" + w1;
    url += "&" + Settings.PH_PARAM_WEATHER_TYPE_2 + "=" + w2;
    url += "&" + Settings.PH_PARAM_WEATHER_TYPE_3 + "=" + w3;

    System.out.println("URL: " + url);

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod(REQ_TYPE);/*  w w  w  .  ja va  2s  . c o m*/

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuilder response = new StringBuilder();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    ArrayList<Phrase> retPhs = new ArrayList<Phrase>();
    JSONArray phs = new JSONArray(response.toString());
    for (int i = 0; i < phs.length(); i++) {
        JSONObject o = phs.getJSONObject(i);
        Phrase p = new Phrase();
        p.setIdphrase(o.getInt(Settings.PH_JSON_ID_PHRASE));
        p.setPhrase(o.getString(Settings.PH_JSON_PHRASE));
        p.setWeathertype(o.getInt(Settings.PH_JSON_WEATHER_TYPE));
        p.setBmirange(o.getInt(Settings.PH_JSON_BMIRANGE));
        p.setChange(o.getInt(Settings.PH_JSON_CHANGE));
        p.setActivity(o.getString(Settings.PH_JSON_ACTIVITY));
        retPhs.add(p);
    }
    return retPhs;
}

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 {//  ww w. jav  a  2  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("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 {//  w  ww . j a  v  a  2 s  .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  a  va  2s  .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("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 w w. j  a v a 2s.  co  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("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 ww  w  . j  a v  a2s.  c  o m

    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.googlecode.goclipse.tooling.oracle.OraclePackageDescribeParser.java

protected ArrayList2<StructureElement> parseElements(JSONArray members, boolean parsingMethods)
        throws JSONException, CommonException {
    ArrayList2<StructureElement> elements = new ArrayList2<>();

    if (members != null) {
        for (int i = 0; i < members.length(); i++) {
            Object object = members.get(i);
            if (object instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) object;
                StructureElement element = parseStructureElement(jsonObject, parsingMethods);
                if (element == null) {
                    continue; // Can happen for external elements
                }/*ww w  .  j  a  va 2  s .c o m*/
                elements.add(element);
            } else {
                throw new CommonException("'members' element is not a JSONObject: " + object);
            }
        }
    }

    Collections.sort(elements, new Comparator<StructureElement>() {
        @Override
        public int compare(StructureElement o1, StructureElement o2) {
            SourceRange sr1 = o1.getSourceRange();
            SourceRange sr2 = o2.getSourceRange();
            int cmp = sr1.getOffset() - sr2.getOffset();
            if (cmp == 0) {
                return o1.getNameSourceRange().getOffset() - o2.getNameSourceRange().getOffset();
            }
            return cmp;
        }
    });

    return elements;
}