Example usage for org.json JSONArray getJSONArray

List of usage examples for org.json JSONArray getJSONArray

Introduction

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

Prototype

public JSONArray getJSONArray(int index) throws JSONException 

Source Link

Document

Get the JSONArray associated with an index.

Usage

From source file:com.phonegap.Storage.java

/**
 * Executes the request and returns PluginResult.
 * /*  w w w. jav a2  s  . com*/
 * @param action       The action to execute.
 * @param args          JSONArry of arguments for the plugin.
 * @param callbackId   The callback id used when calling back into JavaScript.
 * @return             A PluginResult object with a status and message.
 */
public PluginResult execute(String action, JSONArray args, String callbackId) {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";

    try {
        // TODO: Do we want to allow a user to do this, since they could get to other app databases?
        if (action.equals("setStorage")) {
            this.setStorage(args.getString(0));
        } else if (action.equals("openDatabase")) {
            this.openDatabase(args.getString(0), args.getString(1), args.getString(2), args.getLong(3));
        } else if (action.equals("executeSql")) {
            JSONArray a = args.getJSONArray(1);
            int len = a.length();
            String[] s = new String[len];
            for (int i = 0; i < len; i++) {
                s[i] = a.getString(i);
            }
            this.executeSql(args.getString(0), s, args.getString(2));
        }
        return new PluginResult(status, result);
    } catch (JSONException e) {
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }
}

From source file:com.liferay.mobile.android.v62.phone.PhoneService.java

public JSONArray getPhones(String className, long classPK) throws Exception {
    JSONObject _command = new JSONObject();

    try {//from   w  ww.j  av a2  s.c o m
        JSONObject _params = new JSONObject();

        _params.put("className", checkNull(className));
        _params.put("classPK", classPK);

        _command.put("/phone/get-phones", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONArray(0);
}

From source file:salvatejero.cordova.liferay.LiferayPlugin.java

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {

    Log.i(TAG, action);/* w  w w .  j  a v  a2s .  co  m*/
    try {
        if (ACTION_CONNECT.equals(action)) {
            String userName = args.getString(1);
            String serverIp = args.getString(0);
            String password = args.getString(2);

            doConnect(callbackContext, serverIp, userName, password);
            return true;

        } else if (GET_CONNECT.equals(action)) {
            String classNameId = args.getString(0);
            getObjectModel(callbackContext, classNameId, args.getString(1), args.getJSONArray(2));
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        callbackContext.error("Params error");
        Log.e(TAG, "Params error");
        return false;
    }
}

From source file:com.aerhard.oxygen.plugin.dbtagger.util.JsonUtil.java

/**
 * Converts a JSON array to from the JSON server response.
 * //  w w w  .j a v  a 2 s . com
 * @param columns
 *            The number of columns.
 * @param rows
 *            The number of rows.
 * @param dataArray
 *            the input data.
 * 
 * @return the body content
 */
private String[][] convertArray(int rows, int columns, JSONArray dataArray) {
    String[][] resultTable = new String[rows][];
    for (int i = 0; i < rows; i++) {
        JSONArray arr = dataArray.getJSONArray(i);
        List<String> list = new ArrayList<String>();
        if (arr.length() < columns) {
            throw new ArrayStoreException(i18n.getString("jsonUtil.dataColumnError"));
        }
        for (int j = 0; j < columns; j++) {
            list.add(arr.isNull(j) ? "" : arr.getString(j));
        }
        resultTable[i] = list.toArray(new String[columns]);
    }
    return resultTable;
}

From source file:es.prodevelop.gvsig.mini.json.GeoJSONParser.java

private IGeometry decodeGeometry(JSONObject object) throws JSONException {
    IGeometry geom = null;/*from w  w  w  .  ja  v a 2s. co m*/
    LineString[] lineStrings;
    if (object.get("type").equals("MultiLineString")) {
        JSONArray coordinates = object.getJSONArray("coordinates");
        int size = coordinates.length();
        lineStrings = new LineString[size];
        LineString l;
        for (int i = 0; i < size; i++) {
            JSONArray lineStrCoord = coordinates.getJSONArray(i);
            double[][] coords = this.decodeLineStringCoords(lineStrCoord);
            l = new LineString(coords[0], coords[1]);
            lineStrings[i] = l;
        }

        geom = new MultiLineString(lineStrings);
    } else if (object.get("type").equals("LineString")) {
        JSONArray coordinates = object.getJSONArray("coordinates");
        double[][] coords = this.decodeLineStringCoords(coordinates);
        geom = new LineString(coords[0], coords[1]);
    } else if (object.get("type").equals("Point")) {
        JSONArray coordinates = object.getJSONArray("coordinates");
        geom = new Point(coordinates.getDouble(0), coordinates.getDouble(1));
    } else if (object.get("type").equals("Polygon")) {
        JSONArray coordinates = object.getJSONArray("coordinates");
        double[][] coords = this.decodeLineStringCoords(coordinates);
        geom = new Polygon(coords[0], coords[1]);
    }

    return geom;
}

From source file:es.prodevelop.gvsig.mini.json.GeoJSONParser.java

private double[][] decodeLineStringCoords(JSONArray coordinates) throws JSONException {
    int size = coordinates.length();
    double[][] coordsLineStr = new double[2][size];
    for (int i = 0; i < size; i++) {
        JSONArray lineStrCoord = coordinates.getJSONArray(i);
        double[] coords = this.decodeCoordinates(lineStrCoord);
        coordsLineStr[0][i] = coords[0];
        coordsLineStr[1][i] = coords[1];
    }/* w ww.  j a va 2 s  . c  om*/
    return coordsLineStr;
}

From source file:com.liferay.mobile.android.v7.dlfileversion.DLFileVersionService.java

public JSONArray getFileVersions(long fileEntryId, int status) throws Exception {
    JSONObject _command = new JSONObject();

    try {//  w ww .j av  a 2  s.  co  m
        JSONObject _params = new JSONObject();

        _params.put("fileEntryId", fileEntryId);
        _params.put("status", status);

        _command.put("/dlfileversion/get-file-versions", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONArray(0);
}

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

/**
 * Retrieve a JSON array stored at the provided key from the provided JSON array
 * @param array The JSON object to retrieve from
 * @param key The key to retrieve/*from ww w .  j  ava 2s  .  c  o m*/
 * @return the array stored in the key, or null if the key doesn't exist
 */
public static JSONArray safeGetArrayFromArray(JSONArray array, int index) {
    if (array == null || index < 0)
        return null;
    if (index < array.length()) {
        try {
            return array.getJSONArray(index);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get Array from JSONArray at index " + index, e);
        }
    }
    return null;
}

From source file:com.remcob00.cordova.youtube.YouTube.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    cordova.setActivityResultCallback(YouTube.this);

    if (action.equals("playVideo")) {
        if (args.length() == 2) {
            Intent youtubeIntent = YouTubeStandalonePlayer.createVideoIntent(cordova.getActivity(),
                    args.getString(0), args.getString(1));
            cordova.startActivityForResult(this, youtubeIntent, 2300010);

            return true;
        } else if (args.length() == 5) {
            Intent youtubeIntent = YouTubeStandalonePlayer.createVideoIntent(cordova.getActivity(),
                    args.getString(0), args.getString(1), args.getInt(2), args.getBoolean(3),
                    args.getBoolean(4));
            cordova.startActivityForResult(this, youtubeIntent, 2300010);

            return true;
        } else {//w  w  w .java 2  s . co  m
            return false;
        }
    } else if (action.endsWith("playPlaylist")) {
        if (args.length() == 2) {
            Intent youtubeIntent = YouTubeStandalonePlayer.createPlaylistIntent(cordova.getActivity(),
                    args.getString(0), args.getString(1));
            cordova.startActivityForResult(this, youtubeIntent, 2300010);

            return true;
        } else if (args.length() == 6) {
            Intent youtubeIntent = YouTubeStandalonePlayer.createPlaylistIntent(cordova.getActivity(),
                    args.getString(0), args.getString(1), args.getInt(2), args.getInt(3), args.getBoolean(4),
                    args.getBoolean(5));
            cordova.startActivityForResult(this, youtubeIntent, 2300010);

            return true;
        } else {
            return false;
        }
    } else if (action.endsWith("playVideos")) {
        List<String> videos = new ArrayList<String>();

        for (int i = 0; i < args.getJSONArray(1).length(); i++) {
            videos.add(args.getJSONArray(1).getString(i));
        }

        if (args.length() == 2) {
            Intent youtubeIntent = YouTubeStandalonePlayer.createVideosIntent(cordova.getActivity(),
                    args.getString(0), videos);
            cordova.startActivityForResult(this, youtubeIntent, 2300010);

            return true;
        } else if (args.length() == 6) {
            Intent youtubeIntent = YouTubeStandalonePlayer.createVideosIntent(cordova.getActivity(),
                    args.getString(0), videos, args.getInt(2), args.getInt(3), args.getBoolean(4),
                    args.getBoolean(5));
            cordova.startActivityForResult(this, youtubeIntent, 2300010);

            return true;
        } else {
            return false;
        }
    }

    return false;
}

From source file:com.linkedpipes.plugin.loader.dcatAp11ToCkanBatch.DcatAp11ToCkanBatch.java

private Map<String, String> getOrganizations() {
    CloseableHttpResponse queryResponse = null;
    List<String> organizationList = new LinkedList<>();
    Map<String, String> organizations = new HashMap<>();
    HttpGet httpGetOrg = new HttpGet(apiURI + "/organization_list");

    LOG.debug("Querying CKAN for organizations");

    try {//from www  .j  av  a2s  .c om
        queryResponse = queryClient.execute(httpGetOrg);
        if (queryResponse.getStatusLine().getStatusCode() == 200) {
            JSONArray response = new JSONObject(EntityUtils.toString(queryResponse.getEntity()))
                    .getJSONArray("result");
            for (Object o : response) {
                organizationList.add(o.toString());
            }
            LOG.info("Organization list downloaded, found " + organizationList.size() + " organizations.");

        } else {
            String ent = EntityUtils.toString(queryResponse.getEntity());
            LOG.info("Organizations not downloaded: " + ent);
        }
    } catch (Exception e) {
        LOG.error(e.getLocalizedMessage(), e);
    } finally {
        if (queryResponse != null) {
            try {
                queryResponse.close();
            } catch (IOException e) {
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
    }

    LOG.debug("Querying for organization details.");

    for (String organization : organizationList) {
        HttpGet httpGetOrgDetail = new HttpGet(apiURI + "/organization_show?id=" + organization);
        try {
            queryResponse = queryClient.execute(httpGetOrgDetail);
            if (queryResponse.getStatusLine().getStatusCode() == 200) {
                LOG.info("Organization " + organization + " downloaded");

                JSONObject response = new JSONObject(EntityUtils.toString(queryResponse.getEntity()))
                        .getJSONObject("result");
                JSONArray org_extras = response.getJSONArray("extras");
                for (Object extra : org_extras) {
                    String extraKey = ((JSONObject) extra).getString("key");
                    String extraValue = ((JSONObject) extra).getString("value");
                    if (extraKey.equals("uri")) {
                        organizations.put(extraValue, organization);
                        break;
                    }
                }
            } else {
                String ent = EntityUtils.toString(queryResponse.getEntity());
                LOG.info("Organization " + organization + " not downloaded: " + ent);
            }
        } catch (Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        } finally {
            if (queryResponse != null) {
                try {
                    queryResponse.close();
                } catch (IOException e) {
                    LOG.error(e.getLocalizedMessage(), e);
                }
            }
        }
    }

    return organizations;
}