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:ch.lom.clemens.android.bibliography.data.JSONWebConnector.java

public MGroup[] getGroups(MendeleyConnector con) {

    if (!isConnected()) {
        StartActivity.singleton.doAuthentication(StartActivity.MENDELEY_RETRIEVE_COLLECTIONS);
        return null;
    }//from   w  w  w . j a v  a 2 s.c  o  m

    MGroup[] groups = null;

    try {
        String strResponse = connect(
                "http://www.mendeley.com/oapi/library/groups?consumer_key=" + m_consumerkey);

        Log.i("MendeleyComm", strResponse);

        JSONArray items = new JSONArray(strResponse);

        groups = new MGroup[items.length()];

        for (int i = 0; i < items.length(); i++) {
            JSONObject collection = items.getJSONObject(i);

            groups[i] = new MGroup(con);

            groups[i].id = collection.getString("id");
            groups[i].name = collection.getString("name");
            groups[i].type = collection.getString("type");
            groups[i].size = collection.getInt("size");
        }
    } catch (Exception e) {
        Log.e("MendeleyConnector", "Got exception when parsing online group data");
        Log.e("MendeleyConnector", e.getClass().getSimpleName() + ": " + e.getMessage());
    }
    return groups;
}

From source file:org.wso2.carbon.connector.integration.test.gototraining.GototrainingConnectorIntegrationTest.java

/**
 * Positive test case for listTrainings method with mandatory parameters.
 *//* w w w .j av a  2 s  .  co m*/
@Test(groups = { "wso2.esb" }, dependsOnMethods = { "testCreateTrainingWithMandatoryParameters",
        "testCreateTrainingWithOptionalParameters" }, description = "gototraining {listTrainings} integration test with mandatory parameters.")
public void testListTrainingsWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listTrainings");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listTrainings_mandatory.json");

    final String apiEndPoint = apiRequestUrl + "/organizers/" + connectorProperties.getProperty("organizerKey")
            + "/trainings/";

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);

    final JSONArray esbArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    final JSONArray apiArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(esbArray.length(), apiArray.length());
    Assert.assertEquals(esbArray.getJSONObject(0).getString("name"),
            apiArray.getJSONObject(0).getString("name"));
    Assert.assertEquals(esbArray.getJSONObject(0).getString("description"),
            apiArray.getJSONObject(0).getString("description"));
    Assert.assertEquals(esbArray.getJSONObject(0).getJSONArray("times").getJSONObject(0).getString("startDate"),
            apiArray.getJSONObject(0).getJSONArray("times").getJSONObject(0).getString("startDate"));
}

From source file:org.wso2.carbon.connector.integration.test.gototraining.GototrainingConnectorIntegrationTest.java

/**
 * Positive test case for listOrganizers method with mandatory parameters.
 */// ww  w  .ja  v a  2s  .c o m
@Test(dependsOnMethods = { "testCreateTrainingWithMandatoryParameters" }, groups = {
        "wso2.esb" }, description = "gototraining {listOrganizers} integration test with mandatory parameters.")
public void testListOrganizersWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listOrganizers");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listOrganizers_mandatory.json");

    final String apiEndPoint = apiRequestUrl + "/organizers/" + connectorProperties.getProperty("organizerKey")
            + "/trainings/" + connectorProperties.getProperty("trainingKeyMandatory") + "/organizers";

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);

    final JSONArray esbArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    final JSONArray apiArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(esbArray.length(), apiArray.length());
    Assert.assertEquals(esbArray.getJSONObject(0).getString("givenName"),
            apiArray.getJSONObject(0).getString("givenName"));
    Assert.assertEquals(esbArray.getJSONObject(0).getString("email"),
            apiArray.getJSONObject(0).getString("email"));
    Assert.assertEquals(esbArray.getJSONObject(0).getString("surname"),
            apiArray.getJSONObject(0).getString("surname"));
}

From source file:org.wso2.carbon.connector.integration.test.gototraining.GototrainingConnectorIntegrationTest.java

/**
 * Positive test case for listRegistrants method with mandatory parameters.
 *//*w w w  .  j a v a  2s  .  c o  m*/
@Test(groups = { "wso2.esb" }, dependsOnMethods = {
        "testAddRegistrantWithMandatoryParameters" }, description = "gototraining {listRegistrants} integration test with mandatory parameters.")
public void testListRegistrantsWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listRegistrants");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listRegistrants_mandatory.json");

    final String apiEndPoint = apiRequestUrl + "/organizers/" + connectorProperties.getProperty("organizerKey")
            + "/trainings/" + connectorProperties.getProperty("trainingKeyMandatory") + "/registrants";

    final RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET",
            apiRequestHeadersMap);

    final JSONArray esbArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    final JSONArray apiArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(esbArray.length(), apiArray.length());
    Assert.assertEquals(esbArray.getJSONObject(0).getString("givenName"),
            apiArray.getJSONObject(0).getString("givenName"));
    Assert.assertEquals(esbArray.getJSONObject(0).getString("email"),
            apiArray.getJSONObject(0).getString("email"));
    Assert.assertEquals(esbArray.getJSONObject(0).getString("status"),
            apiArray.getJSONObject(0).getString("status"));
}

From source file:ai.susi.mind.SusiCognition.java

public SusiCognition(final SusiMind mind, final String query, int timezoneOffset, double latitude,
        double longitude, int maxcount, ClientIdentity identity) {
    this.json = new JSONObject(true);

    // get a response from susis mind
    String client = identity.getClient();
    this.setQuery(query);
    this.json.put("count", maxcount);
    SusiThought observation = new SusiThought();
    observation.addObservation("timezoneOffset", Integer.toString(timezoneOffset));

    if (!Double.isNaN(latitude) && !Double.isNaN(longitude)) {
        observation.addObservation("latitude", Double.toString(latitude));
        observation.addObservation("longitude", Double.toString(longitude));
    }/*w w w . ja v a  2s.  c o  m*/
    this.json.put("client_id", Base64.getEncoder().encodeToString(UTF8.getBytes(client)));
    long query_date = System.currentTimeMillis();
    this.json.put("query_date", DateParser.utcFormatter.print(query_date));

    // compute the mind reaction
    List<SusiArgument> dispute = mind.react(query, maxcount, client, observation);
    long answer_date = System.currentTimeMillis();

    // store answer and actions into json
    this.json.put("answers", new JSONArray(
            dispute.stream().map(argument -> argument.finding(client, mind)).collect(Collectors.toList())));
    this.json.put("answer_date", DateParser.utcFormatter.print(answer_date));
    this.json.put("answer_time", answer_date - query_date);
    this.json.put("language", "en");
}

From source file:com.googlecode.android_scripting.facade.ui.UiFacade.java

/**
 * This will override the default behaviour of keys while in the fullscreen mode. ie:
 * //from  w ww  .j a va2 s .  c o  m
 * <pre>
 *   droid.fullKeyOverride([24,25],True)
 * </pre>
 * 
 * This will override the default behaviour of the volume keys (codes 24 and 25) so that they do
 * not actually adjust the volume. <br>
 * Returns a list of currently overridden keycodes.
 */
@Rpc(description = "Override default key actions")
public JSONArray fullKeyOverride(
        @RpcParameter(name = "keycodes", description = "List of keycodes to override") JSONArray keycodes,
        @RpcParameter(name = "enable", description = "Turn overriding or off") @RpcDefault(value = "true") Boolean enable)
        throws JSONException {
    for (int i = 0; i < keycodes.length(); i++) {
        int value = (int) keycodes.getLong(i);
        if (value > 0) {
            if (enable) {
                if (!mOverrideKeys.contains(value)) {
                    mOverrideKeys.add(value);
                }
            } else {
                int index = mOverrideKeys.indexOf(value);
                if (index >= 0) {
                    mOverrideKeys.remove(index);
                }
            }
        }
    }
    if (mFullScreenTask != null) {
        mFullScreenTask.setOverrideKeys(mOverrideKeys);
    }
    return new JSONArray(mOverrideKeys);
}

From source file:org.huahinframework.manager.util.JobUtils.java

/**
 * @param state/*from   w ww. j a  va  2 s .c  o m*/
 * @param conf
 * @return {@link JSONArray}
 * @throws JSONException
 * @throws InterruptedException
 */
@SuppressWarnings("unchecked")
public static JSONArray getJobs(State state, JobConf conf) throws JSONException, InterruptedException {
    JSONArray jsonArray = null;
    try {
        jsonArray = new JSONArray(listJob(state, conf));
    } catch (IOException e) {
        e.printStackTrace();
        log.error(e);
        Map<String, String> status = new HashMap<String, String>();
        status.put(Response.STATUS, e.getMessage());
        jsonArray = new JSONArray(Arrays.asList(status));
    }

    return jsonArray;
}

From source file:com.googlecode.CallerLookup.Main.java

public void parseLookupEntries() {
    mLookupEntries = new HashMap<String, LookupEntry>();
    mUserLookupEntries = new HashMap<String, LookupEntry>();

    boolean updateFound = false;
    for (String fileName : getApplicationContext().fileList()) {
        if (fileName.equals(UPDATE_FILE)) {
            try {
                FileInputStream file = getApplicationContext().openFileInput(UPDATE_FILE);
                XmlPullParser xml = Xml.newPullParser();
                xml.setInput(file, null);
                parseLookupEntries(xml, mLookupEntries);
                file.close();/*from  w  w w .j a v  a2  s  . co m*/
                updateFound = true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (fileName.equals(SAVED_FILE)) {
            try {
                FileInputStream file = getApplicationContext().openFileInput(SAVED_FILE);
                InputStreamReader reader = new InputStreamReader(file);
                char[] content = new char[8000];
                reader.read(content);
                JSONArray userLookupEntries = new JSONArray(new String(content));
                int count = userLookupEntries.length();
                for (int i = 0; i < count; i++) {
                    JSONObject userLookupEntry = userLookupEntries.getJSONObject(i);
                    mUserLookupEntries.put(userLookupEntry.getString("name"), new LookupEntry(userLookupEntry));
                }
                file.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    if (!updateFound) {
        XmlResourceParser xml = getApplicationContext().getResources().getXml(R.xml.lookups);
        parseLookupEntries(xml, mLookupEntries);
        xml.close();
    }
}

From source file:ti.mobileapptracker.MobileapptrackerModule.java

private List<MATEventItem> convertToMATEventItems(Object[] arrItemMaps) {
    List<MATEventItem> listItems = new ArrayList<MATEventItem>();

    try {/*  www . jav  a 2s.c om*/
        JSONArray arr = new JSONArray(Arrays.toString(arrItemMaps));

        for (int i = 0; i < arr.length(); i++) {
            JSONObject item = arr.getJSONObject(i);

            String itemName = item.getString("item");
            int quantity = 0;
            double unitPrice = 0;
            double revenue = 0;
            String attribute1 = null;
            String attribute2 = null;
            String attribute3 = null;
            String attribute4 = null;
            String attribute5 = null;

            if (item.has("quantity")) {
                quantity = item.getInt("quantity");
            }
            if (item.has("unit_price")) {
                unitPrice = item.getDouble("unit_price");
            }
            if (item.has("revenue")) {
                revenue = item.getDouble("revenue");
            }

            if (item.has("attribute_sub1")) {
                attribute1 = item.getString("attribute_sub1");
            }
            if (item.has("attribute_sub2")) {
                attribute2 = item.getString("attribute_sub2");
            }
            if (item.has("attribute_sub3")) {
                attribute3 = item.getString("attribute_sub3");
            }
            if (item.has("attribute_sub4")) {
                attribute4 = item.getString("attribute_sub4");
            }
            if (item.has("attribute_sub5")) {
                attribute5 = item.getString("attribute_sub5");
            }

            MATEventItem eventItem = new MATEventItem(itemName, quantity, unitPrice, revenue, attribute1,
                    attribute2, attribute3, attribute4, attribute5);
            listItems.add(eventItem);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return listItems;
}

From source file:org.everit.json.schema.IntegrationTest.java

private static JSONArray loadTests(final InputStream input) {
    return new JSONArray(new JSONTokener(input));
}