Example usage for org.json JSONArray JSONArray

List of usage examples for org.json JSONArray JSONArray

Introduction

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

Prototype

public JSONArray(Object array) throws JSONException 

Source Link

Document

Construct a JSONArray from an array

Usage

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

public void fillStyles() {
    mStyles = new ArrayList<>();
    try {//from   ww w.jav  a 2 s.c o m
        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.trafficspaces.api.controller.Connector.java

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

public List find(Properties params) throws IOException, TrafficspacesAPIException {
    String jsonStr = sendRequest(resourcePath + "?" + toQueryString(params), "application/json");
    try {// w  w  w .  ja  v a2 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: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);//from w w w. j a va2s. c  om

    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.wabbit.libraries.remoting.HttpHelper.java

static public JSONArray loadJSON(String url) {
    HttpURLConnection connection = null;
    JSONArray json = null;/*from   w  w w  . ja v  a 2 s  .c  o m*/
    InputStream is = null;

    try {
        connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setConnectTimeout(5000);

        is = new BufferedInputStream(connection.getInputStream());
        json = new JSONArray(convertStreamToString(is));
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (JSONException je) {
        je.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        if (connection != null) {
            connection.disconnect();
        }
    }

    return json;
}

From source file:org.loklak.api.iot.YahiHazeServlet.java

public static JSONArray readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {/*from   www . j  a v  a  2s .co m*/
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONArray json = new JSONArray(jsonText);
        return json;
    } finally {
        is.close();
    }
}

From source file:org.protorabbit.model.impl.DefaultEngine.java

static IParameter getParameter(String text, Character lastToken) {
    Object value = null;//from  ww  w  .  ja va2  s . c  o  m
    text = text.trim();
    int type = -1;
    if (lastToken != null && lastToken.charValue() == '\"') {
        type = IParameter.STRING;
        // set the value to not include the quotes
        value = text.substring(1, text.length() - 1);
    } else if ("true".equals(text) || "false".equals(text)) {
        type = IParameter.BOOLEAN;
        value = new Boolean(("true".equals(text)));
    } else if ("null".equals(text)) {
        type = IParameter.NULL;
    } else if (text.startsWith("{") && text.endsWith("}")) {
        try {
            value = new JSONObject(text);
            type = IParameter.OBJECT;
        } catch (JSONException e) {
            throw new RuntimeException("Error parsing JSON parameter " + text);
        }
    } else if (text.startsWith("[") && text.endsWith("]")) {
        try {
            value = new JSONArray(text);
            type = IParameter.ARRAY;
        } catch (JSONException e) {
            throw new RuntimeException("Error parsing JSON parameter " + text);
        }
    } else {
        try {
            NumberFormat nf = NumberFormat.getInstance();
            value = nf.parse(text);
            type = IParameter.NUMBER;
        } catch (ParseException pe) {
            // do nothing
        }
    }
    if (type != -1) {
        return new Parameter(type, value);
    } else {
        throw new RuntimeException("Error parsing parameter " + text);
    }
}

From source file:com.idean.atthack.api.Param.java

public void addToJsonAsObject(JSONObject parent, Bundle params) {
    String defaultVal = params.getString(name());
    if (defaultVal == null) {
        Log.w(TAG, "Unable to add this param " + this + " as a JSON object");
        return;/*from  w  w  w. j  ava  2 s  .c  o m*/
    }
    try {
        JSONObject obj = new JSONObject(defaultVal);
        parent.put(name(), obj);
    } catch (JSONException e) {
        try {
            JSONArray arr = new JSONArray(defaultVal);
            parent.put(name(), arr);
        } catch (JSONException e1) {
            Log.w(TAG, "Unable to parse as JSON " + defaultVal);
        }
    }

}

From source file:com.ecml.RecentSongsActivity.java

private void loadFileList() {
    filelist = new ArrayList<FileUri>();
    SharedPreferences settings = getSharedPreferences("midisheetmusic.recentFiles", 0);
    String recentFilesString = settings.getString("recentFiles", null);
    if (recentFilesString == null) {
        return;/*from  w  ww .j av  a 2 s.c  o m*/
    }
    try {
        JSONArray jsonArray = new JSONArray(recentFilesString);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject obj = jsonArray.getJSONObject(i);
            FileUri file = FileUri.fromJson(obj, this);
            if (file != null) {
                filelist.add(file);
            }
        }
    } catch (Exception e) {
    }
}

From source file:org.jboss.aerogear.cordova.push.PushPlugin.java

private List<String> convert(String categories) throws JSONException {
    List<String> categoryList = null;
    if (categories != null) {
        categoryList = new ArrayList<String>();
        final JSONArray jsonArray = new JSONArray(categories);
        for (int i = 0; i < jsonArray.length(); i++) {
            categoryList.add(jsonArray.getString(i));
        }/*from   www .j  ava 2s .  c  om*/
    }
    return categoryList;
}

From source file:org.jboss.aerogear.cordova.push.PushPlugin.java

/**
 * Serializes a bundle to JSON.//from  w w  w. j  ava2  s .c  om
 * @param message to be serialized
 */
private static JSONObject convertBundleToJson(Bundle message) {
    try {
        JSONObject json;
        json = new JSONObject();

        JSONObject jsondata = new JSONObject();
        for (String key : message.keySet()) {
            Object value = message.get(key);

            // System data from Android
            if (key.equals("from") || key.equals("collapse_key")) {
                json.put(key, value);
            } else if (key.equals("foreground")) {
                json.put(key, message.getBoolean("foreground"));
            } else if (key.equals("coldstart")) {
                json.put(key, message.getBoolean("coldstart"));
            } else {
                // Maintain backwards compatibility
                if (key.equals("message") || key.equals("msgcnt") || key.equals("sound")
                        || key.equals("alert")) {
                    json.put(key, value);
                }

                if (value instanceof String) {
                    // Try to figure out if the value is another JSON object

                    String strValue = (String) value;
                    if (strValue.startsWith("{")) {
                        try {
                            JSONObject json2 = new JSONObject(strValue);
                            jsondata.put(key, json2);
                        } catch (Exception e) {
                            jsondata.put(key, value);
                        }
                        // Try to figure out if the value is another JSON array
                    } else if (strValue.startsWith("[")) {
                        try {
                            JSONArray json2 = new JSONArray(strValue);
                            jsondata.put(key, json2);
                        } catch (Exception e) {
                            jsondata.put(key, value);
                        }
                    } else {
                        jsondata.put(key, value);
                    }
                }
            }
        } // while
        json.put("payload", jsondata);

        Log.v(TAG, "extrasToJSON: " + json.toString());

        return json;
    } catch (JSONException e) {
        Log.e(TAG, "extrasToJSON: JSON exception");
    }
    return null;
}