Example usage for org.json JSONArray getJSONObject

List of usage examples for org.json JSONArray getJSONObject

Introduction

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

Prototype

public JSONObject getJSONObject(int index) throws JSONException 

Source Link

Document

Get the JSONObject associated with an index.

Usage

From source file:com.vsu.bruteremote.FileBrowser.java

private FileBrowserItem[] parse(String jString) throws Exception {
    List<FileBrowserItem> items = new ArrayList<FileBrowserItem>();

    JSONObject jObject = new JSONObject(jString);

    String dir = jObject.getString("dir");
    JSONArray subdirArray = jObject.getJSONArray("subdir");
    JSONArray fileArray = jObject.getJSONArray("file");

    for (int ix = 0; ix < subdirArray.length(); ix++) {
        JSONObject obj = subdirArray.getJSONObject(ix);

        String display = obj.getString("display");
        String name = obj.getString("name");
        String path = obj.getString("path");

        items.add(new FileBrowserItem(display, name, dir, path, true));
    }//from   www.  j a  v a 2  s.  com

    for (int ix = 0; ix < fileArray.length(); ix++) {
        JSONObject obj = fileArray.getJSONObject(ix);

        String display = obj.getString("display");
        String name = obj.getString("name");
        String path = obj.getString("path");

        items.add(new FileBrowserItem(display, name, dir, path, false));
    }

    return items.toArray(new FileBrowserItem[0]);
}

From source file:hongik.android.project.best.StoreReviewActivity.java

public void drawTable() throws Exception {
    String query = "func=morereview" + "&license=" + license;
    DBConnector conn = new DBConnector(query);
    conn.start();//w  w w  .  j  a va  2s  .  c om

    conn.join();
    JSONObject jsonResult = conn.getResult();
    boolean result = jsonResult.getBoolean("result");

    if (!result) {
        return;
    }

    String storeName = jsonResult.getString("sname");
    ((TextViewPlus) findViewById(R.id.storereview_storename)).setText(storeName);

    JSONArray review = null;
    if (!jsonResult.isNull("review")) {
        review = jsonResult.getJSONArray("review");
    }

    //Draw Review Table
    if (review != null) {
        TableRow motive = (TableRow) reviewTable.getChildAt(1);

        for (int i = 0; i < review.length(); i++) {
            JSONObject json = review.getJSONObject(i);
            final String[] elements = new String[4];
            elements[0] = Double.parseDouble(json.getString("GRADE")) + "";
            elements[1] = json.getString("NOTE");
            elements[2] = json.getString("CID#");
            elements[3] = json.getString("DAY");

            TableRow tbRow = new TableRow(this);
            TextViewPlus[] tbCols = new TextViewPlus[4];

            if (elements[1].length() > 14)
                elements[1] = elements[1].substring(0, 14) + "...";

            for (int j = 0; j < 4; j++) {
                tbCols[j] = new TextViewPlus(this);
                tbCols[j].setText(elements[j]);
                tbCols[j].setLayoutParams(motive.getChildAt(j).getLayoutParams());
                tbCols[j].setGravity(Gravity.CENTER);
                tbCols[j].setTypeface(Typeface.createFromAsset(tbCols[j].getContext().getAssets(),
                        "InterparkGothicBold.ttf"));
                tbCols[j].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent reviewIntent = new Intent(originActivity, ReviewDetailActivity.class);
                        reviewIntent.putExtra("ACCESS", "STORE");
                        reviewIntent.putExtra("CID", elements[2]);
                        reviewIntent.putExtra("LICENSE", license);
                        Log.i("StoreReview", "StartActivity");
                        startActivity(reviewIntent);
                    }
                });

                Log.i("StoreMenu", "COL" + j + ":" + elements[j]);
                tbRow.addView(tbCols[j]);
            }
            reviewTable.addView(tbRow);
        }
    }
    reviewTable.removeViewAt(1);
}

From source file:de.duenndns.ssl.MemorizingTrustManager.java

private List<String> getPoshFingerprintsFromServer(String domain, String url, int maxTtl, boolean followUrl) {
    Log.d("mtm", "downloading json for " + domain + " from " + url);
    try {/*from  w  w  w .  j  a v a2  s.  c om*/
        List<String> results = new ArrayList<>();
        HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder builder = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            builder.append(inputLine);
        }
        JSONObject jsonObject = new JSONObject(builder.toString());
        in.close();
        int expires = jsonObject.getInt("expires");
        if (expires <= 0) {
            return new ArrayList<>();
        }
        if (maxTtl >= 0) {
            expires = Math.min(maxTtl, expires);
        }
        String redirect;
        try {
            redirect = jsonObject.getString("url");
        } catch (JSONException e) {
            redirect = null;
        }
        if (followUrl && redirect != null && redirect.toLowerCase().startsWith("https")) {
            return getPoshFingerprintsFromServer(domain, redirect, expires, false);
        }
        JSONArray fingerprints = jsonObject.getJSONArray("fingerprints");
        for (int i = 0; i < fingerprints.length(); i++) {
            JSONObject fingerprint = fingerprints.getJSONObject(i);
            String sha256 = fingerprint.getString("sha-256");
            if (sha256 != null) {
                results.add(sha256);
            }
        }
        writeFingerprintsToCache(domain, results, 1000L * expires + System.currentTimeMillis());
        return results;
    } catch (Exception e) {
        Log.d("mtm", "error fetching posh " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:org.ESLM.Parser.JSONProtoLessonParser.java

private Instruction[] parseExercises(JSONArray jsonExercises) throws BadFormedJSONExerciseException {
    int numberOfExercises = jsonExercises.length();
    Instruction[] instructions = new Instruction[numberOfExercises];
    for (int i = 0; i < numberOfExercises; i++) {
        JSONObject jsonExercise = jsonExercises.getJSONObject(i);
        instructions[i] = ExerciseFactory.createInstruction(jsonExercise);
    }/*from  ww  w.ja  v a2s. c o m*/
    return instructions;
}

From source file:nl.hnogames.domoticzapi.Parsers.CameraParser.java

@Override
public void parseResult(String result) {
    try {// w  w w .  j  av  a  2s  .c o  m
        JSONArray jsonArray = new JSONArray(result);
        ArrayList<CameraInfo> mCameras = new ArrayList<>();
        if (jsonArray.length() > 0) {
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject row = jsonArray.getJSONObject(i);
                CameraInfo cameraInfo = new CameraInfo(row);
                cameraInfo.setSnapShotURL(this.domoticz.getSnapshotUrl(cameraInfo));
                if (cameraInfo.getEnabled())
                    mCameras.add(cameraInfo);
            }
        }
        receiver.OnReceiveCameras(mCameras);
    } catch (JSONException e) {
        Log.e(TAG, "CameraParser JSON exception");
        e.printStackTrace();
        receiver.onError(e);
    }
}

From source file:com.nextgis.maplib.datasource.ngw.LayerWithStyles.java

public void fillStyles() {
    mStyles = new ArrayList<>();
    try {//from  w ww .  j a v a  2s  . c  om
        String sURL = mConnection.getURL() + "/resource/" + mRemoteId + "/child/";
        HttpGet get = new HttpGet(sURL);
        get.setHeader("Cookie", mConnection.getCookie());
        get.setHeader("Accept", "*/*");
        HttpResponse response = mConnection.getHttpClient().execute(get);
        HttpEntity entity = response.getEntity();
        JSONArray children = new JSONArray(EntityUtils.toString(entity));
        for (int i = 0; i < children.length(); i++) {
            //Only store style id
            //To get more style properties need to create style class extended from Resource
            //Style extends Resource
            //mStyles.add(new Style(styleObject, mConnection);
            JSONObject styleObject = children.getJSONObject(i);
            JSONObject JSONResource = styleObject.getJSONObject("resource");
            long remoteId = JSONResource.getLong("id");
            mStyles.add(remoteId);
        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve a JSON object stored at the provided index from the provided JSON Array object
 * @param obj The JSON Array object to retrieve from
 * @param key The index to retrieve//from   www .  ja  v a2  s .co  m
 * @return the JSON object stored at the index, or null if the index doesn't exist
 */
public static JSONObject safeGetJSONObjectFromArray(JSONArray obj, int index) {
    if (obj == null || index < 0)
        return null;
    if (obj.length() > index) {
        try {
            return obj.getJSONObject(index);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get object from JSONArray from at index " + index, e);
        }
    }
    return null;
}

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  ww.ja  v a 2 s.  com*/
        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:io.winch.phonegap.plugin.WinchPlugin.java

private void sync(JSONArray args) {
    try {/*from w ww.  ja  v  a2s  .co  m*/
        List<SyncElement> syncElements = new ArrayList<SyncElement>();
        JSONObject params = args.getJSONObject(0);
        Iterator<?> iter = params.keys();

        // iterate to retrieve sync pairs
        while (iter.hasNext()) {
            String namespace = (String) iter.next();
            String syncOption = params.getString(namespace);
            SyncElement se = new SyncElement(namespace, syncOption);
            syncElements.add(se);
        }

        mWinch.sync(this, syncElements);
    } catch (JSONException e1) {
        e1.printStackTrace();
    } catch (WinchError e) {
        e.printStackTrace();
        PluginResult r = buildErrorResult(e.getErrorCode(), e.getMessage());
        this.syncCallback.sendPluginResult(r);
    }
}

From source file:io.winch.phonegap.plugin.WinchPlugin.java

private void load(JSONArray args) {
    try {/*  w  w  w  . ja  v a2  s.  c  om*/
        List<LoadElement> loadElements = new ArrayList<LoadElement>();
        JSONObject params = args.getJSONObject(0);
        Iterator<?> iter = params.keys();

        // iterate to retrieve load pairs
        while (iter.hasNext()) {
            String namespace = (String) iter.next();
            String key = params.getString(namespace);
            LoadElement le = new LoadElement(namespace, key);
            loadElements.add(le);
        }

        mWinch.load(this, loadElements);
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (WinchError e) {
        e.printStackTrace();
        PluginResult r = buildErrorResult(e.getErrorCode(), e.getMessage());
        this.loadCallback.sendPluginResult(r);
    }
}