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:org.wso2.carbon.connector.integration.test.pivotaltracker.PivotaltrackerConnectorIntegrationTest.java

/**
 * Positive test case for getProjectLabels method with mandatory parameters.
 * /*from  www.  j  a v a2 s. c  o m*/
 * @throws JSONException
 * @throws IOException
 */
@Test(groups = { "wso2.esb" }, dependsOnMethods = {
        "testCreateLabelForProjectWithMandatoryParameters" }, description = "pivotaltracker {getProjectLabels} integration test with mandatory parameters.")
public void testGetProjectLabelsWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:getProjectLabels");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_getProjectLabels_mandatory.json");
    JSONArray esbOutputArray = new JSONArray(esbRestResponse.getBody().getString("output"));

    final String apiEndpoint = apiEndpointUrl + "/projects/" + connectorProperties.getProperty("projectId")
            + "/labels";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndpoint, "GET", apiRequestHeadersMap);
    JSONArray apiOutputArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(esbOutputArray.length(), apiOutputArray.length());
    Assert.assertEquals(esbOutputArray.getJSONObject(0).getString("id"),
            apiOutputArray.getJSONObject(0).getString("id"));
    Assert.assertEquals(esbOutputArray.getJSONObject(0).getString("name"),
            apiOutputArray.getJSONObject(0).getString("name"));
    Assert.assertEquals(esbOutputArray.getJSONObject(0).getString("project_id"),
            apiOutputArray.getJSONObject(0).getString("project_id"));
    Assert.assertEquals(esbOutputArray.getJSONObject(0).getString("kind"),
            apiOutputArray.getJSONObject(0).getString("kind"));
    Assert.assertEquals(esbOutputArray.getJSONObject(0).getString("created_at"),
            apiOutputArray.getJSONObject(0).getString("created_at"));

}

From source file:org.wso2.carbon.connector.integration.test.pivotaltracker.PivotaltrackerConnectorIntegrationTest.java

/**
 * Positive test case for createLabelForStory method with mandatory parameters.
 * //w w  w  . j  av  a2 s .  co m
 * @throws JSONException
 * @throws IOException
 */
@Test(groups = { "wso2.esb" }, dependsOnMethods = { "testCreateProjectWithMandatoryParameters",
        "testCreateStoryWithMandatoryParameters" }, description = "pivotaltracker {createLabelForStory} integration test with mandatory parameters.")
public void testCreateLabelForStoryWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:createLabelForStory");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_createLabelForStory_mandatory.json");
    final String labelId = esbRestResponse.getBody().getString("id");

    final String apiEndpoint = apiEndpointUrl + "/projects/" + connectorProperties.getProperty("projectId")
            + "/stories/" + connectorProperties.getProperty("storyId") + "/labels";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndpoint, "GET", apiRequestHeadersMap);
    JSONArray apiOutputArray = new JSONArray(apiRestResponse.getBody().getString("output"));
    JSONObject lableObj = null;
    for (int i = 0; i < apiOutputArray.length(); i++) {
        lableObj = apiOutputArray.getJSONObject(i);
        if (labelId.equals(lableObj.getString("id"))) {
            break;
        }
    }
    Assert.assertEquals(connectorProperties.getProperty("labelName"), lableObj.getString("name"));

}

From source file:org.wso2.carbon.connector.integration.test.pivotaltracker.PivotaltrackerConnectorIntegrationTest.java

/**
 * Positive test case for listStories method with mandatory parameters.
 * /*from ww  w  .  j a va2s  .c  o  m*/
 * @throws IOException
 * @throws JSONException
 */
@Test(groups = { "wso2.esb" }, dependsOnMethods = { "testCreateProjectWithMandatoryParameters",
        "testCreateStoryWithOptionalParameters" }, description = "pivotaltracker {listStories} integration test with mandatory parameters.")
public void testListStoriesWithMandatoryParameters() throws IOException, JSONException {

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

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listStories_mandatory.json");
    JSONArray esbUserStoryArray = new JSONArray(esbRestResponse.getBody().getString("output"));

    String apiEndPoint = apiEndpointUrl + "/projects/" + connectorProperties.getProperty("projectId")
            + "/stories";

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray apiUserStoryArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(esbUserStoryArray.length(), apiUserStoryArray.length());
    Assert.assertEquals(esbUserStoryArray.getJSONObject(0).getString("id"),
            apiUserStoryArray.getJSONObject(0).getString("id"));
    Assert.assertEquals(esbUserStoryArray.getJSONObject(0).getString("name"),
            apiUserStoryArray.getJSONObject(0).getString("name"));
    Assert.assertEquals(esbUserStoryArray.getJSONObject(0).getString("created_at"),
            apiUserStoryArray.getJSONObject(0).getString("created_at"));
    Assert.assertEquals(esbUserStoryArray.getJSONObject(0).getString("updated_at"),
            apiUserStoryArray.getJSONObject(0).getString("updated_at"));
}

From source file:org.wso2.carbon.connector.integration.test.pivotaltracker.PivotaltrackerConnectorIntegrationTest.java

/**
 * Positive test case for listStories method with optional parameters.
 * /*from   w  w  w  .  j  a va2  s .  co  m*/
 * @throws IOException
 * @throws JSONException
 */
@Test(groups = { "wso2.esb" }, dependsOnMethods = { "testCreateProjectWithMandatoryParameters",
        "testCreateStoryWithOptionalParameters" }, description = "pivotaltracker {listStories} integration test with optional parameters.")
public void testListStoriesWithOptionalParameters() throws IOException, JSONException {

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

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listStories_optional.json");
    JSONArray esbUserStoryArray = new JSONArray(esbRestResponse.getBody().getString("output"));

    URLEncoder encoder = new URLEncoder();
    final String state = encoder.encode("accepted");

    String apiEndPoint = apiEndpointUrl + "/projects/" + connectorProperties.getProperty("projectId")
            + "/stories?offset=0&limit=10&created_before=" + connectorProperties.getProperty("createdBefore")
            + "&created_after=" + connectorProperties.getProperty("createdAfter") + "&with_state=" + state;

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray apiUserStoryArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(esbUserStoryArray.length(), apiUserStoryArray.length());
    Assert.assertEquals(esbUserStoryArray.getJSONObject(0).getString("id"),
            apiUserStoryArray.getJSONObject(0).getString("id"));
    Assert.assertEquals(esbUserStoryArray.getJSONObject(0).getString("name"),
            apiUserStoryArray.getJSONObject(0).getString("name"));
    Assert.assertEquals(esbUserStoryArray.getJSONObject(0).getString("created_at"),
            apiUserStoryArray.getJSONObject(0).getString("created_at"));
    Assert.assertEquals(esbUserStoryArray.getJSONObject(0).getString("updated_at"),
            apiUserStoryArray.getJSONObject(0).getString("updated_at"));

}

From source file:org.wso2.carbon.connector.clevertim.CreateContact.java

/**
 * Create JSON request for CreateContact.
 *
 * @return JSON payload.//  w  w  w . ja  v  a  2 s . c o  m
 * @throws JSONException thrown when parsing JSON String.
 */
private String getJsonPayload() throws JSONException {

    JSONObject jsonPayload = new JSONObject();

    String firstName = (String) messageContext.getProperty(Constants.FIRST_NAME);
    if (firstName != null && !firstName.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.FIRST_NAME, firstName);
    }
    String lastName = (String) messageContext.getProperty(Constants.LAST_NAME);
    if (lastName != null && !lastName.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.LAST_NAME, lastName);
    }
    String title = (String) messageContext.getProperty(Constants.TITLE);
    if (title != null && !title.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.TITLE, title);
    }
    String description = (String) messageContext.getProperty(Constants.DESCRIPTION);
    if (description != null && !description.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.DESCRIPTION, description);
    }
    String isCompany = (String) messageContext.getProperty(Constants.IS_COMPANY);
    if (isCompany != null && !isCompany.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.IS_COMPANY, isCompany);
    }
    String companyId = (String) messageContext.getProperty(Constants.COMPANY_ID);
    if (companyId != null && !companyId.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.COMPANY_ID, companyId);
    }
    String email = (String) messageContext.getProperty(Constants.EMAIL);
    if (email != null && !email.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.EMAIL, new JSONArray(email));
    }
    String phones = (String) messageContext.getProperty(Constants.PHONES);
    if (phones != null && !phones.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.PHONES, new JSONArray(phones));
    }
    String website = (String) messageContext.getProperty(Constants.WEBSITE);
    if (website != null && !website.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.WEBSITE, new JSONArray(website));
    }
    String address = (String) messageContext.getProperty(Constants.ADDRESS);
    if (address != null && !address.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.ADDRESS, address);
    }
    String city = (String) messageContext.getProperty(Constants.CITY);
    if (city != null && !city.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.CITY, city);
    }
    String postCode = (String) messageContext.getProperty(Constants.POST_CODE);
    if (postCode != null && !postCode.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.POST_CODE, postCode);
    }
    String country = (String) messageContext.getProperty(Constants.COUNTRY);
    if (country != null && !country.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.COUNTRY, country);
    }
    String socialMediaIds = (String) messageContext.getProperty(Constants.SOCIAL_MEDIA_IDS);
    if (socialMediaIds != null && !socialMediaIds.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.SOCIAL_MEDIA_IDS, new JSONArray(socialMediaIds));
    }
    String customerType = (String) messageContext.getProperty(Constants.CUSTOMER_TYPE);
    if (customerType != null && !customerType.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.CUSTOMER_TYPE, customerType);
    }
    String customFields = (String) messageContext.getProperty(Constants.CUSTOM_FIELDS);
    if (customFields != null && !customFields.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.CUSTOM_FIELD, new JSONObject(customFields));
    }
    String tags = (String) messageContext.getProperty(Constants.TAGS);
    if (tags != null && !tags.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.TAGS, new JSONArray(tags));
    }

    return jsonPayload.toString();
}

From source file:com.ct.speech.HintReceiver.java

@Override
public void onReceive(Context context, Intent intent) {

    if (getResultCode() != Activity.RESULT_OK) {
        return;//from  w  ww  .ja v a 2  s.c o m
    }
    // the list of supported languages.
    ArrayList<CharSequence> hints = getResultExtras(true)
            .getCharSequenceArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);

    // Convert the map to json
    JSONArray languageArray = new JSONArray(hints);
    PluginResult result = new PluginResult(PluginResult.Status.OK, languageArray);
    result.setKeepCallback(false);
    // speechRecognizer.callbackId = "";
    speechRecognizerPlugin.success(result, "");
}

From source file:com.openquartz.glassmemo.Utils.java

public static void commitNewMemoList(final Context context, final String key, final List<String> memoList) {
    SharedPreferences.Editor sharedPrefEditor = PreferenceManager.getDefaultSharedPreferences(context).edit();

    if (!memoList.isEmpty()) {
        sharedPrefEditor.putString(key, new JSONArray(memoList).toString());
    } else {/*from   w ww  . j ava 2s . co  m*/
        sharedPrefEditor.putString(key, null);
    }

    sharedPrefEditor.commit();
}

From source file:com.openquartz.glassmemo.Utils.java

/**
 * Gets the arraylist from shared preferences 
 * @param context//from   ww  w. ja  va  2 s . c om
 * @param key
 * @return
 */
public static ArrayList<String> getStringArrayPref(final Context context, String key) {

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    String json = sharedPref.getString(key, null);
    ArrayList<String> memoList = new ArrayList<String>();

    if (json != null) {
        try {
            JSONArray a = new JSONArray(json);

            for (int i = 0; i < a.length(); i++) {
                memoList.add(a.optString(i));
            }
        } catch (JSONException e) {
        }
    }

    return memoList;
}

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

@Test(expected = ValidationException.class)
public void failure() {
    EnumSchema subject = subject();/*from www  . ja v a2 s .  c  o  m*/
    subject.validate(new JSONArray("[1]"));
}

From source file:com.andybotting.tramhunter.objects.FavouriteList.java

/**
 * Get the favourite items/*  www.  j a  v a  2  s .c o  m*/
 * @return 
 */
public ArrayList<Favourite> getFavouriteItems() {

    ArrayList<Favourite> favourites = new ArrayList<Favourite>();
    JSONArray favouriteJSONArray = null;
    TramHunterDB db = new TramHunterDB();

    // Fetch JSON favourite stops string from preferences
    String favouriteString = mPreferenceHelper.getStarredStopsString();
    if (LOGV)
        Log.i(TAG, "Parsing favourite string: " + favouriteString);

    // Check to see if we even have any favourites
    if (favouriteString.length() > 1) {

        // Convert any old favourites - if not a JSON Array
        if (!favouriteString.contains("["))
            favouriteString = convertOldFavourites(favouriteString);

        try {
            favouriteJSONArray = new JSONArray(favouriteString);

            for (int i = 0; i < favouriteJSONArray.length(); i++) {

                JSONObject favouriteJSONObject = favouriteJSONArray.getJSONObject(i);

                int tramTrackerID = favouriteJSONObject.optInt("stop");
                int routeID = favouriteJSONObject.optInt("route", -1);
                String name = favouriteJSONObject.optString("name", null);

                Stop stop = db.getStop(tramTrackerID);
                Route route = null;

                if (routeID != -1)
                    route = db.getRoute(routeID);

                favourites.add(new Favourite(stop, route, name));
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    db.close();
    return favourites;
}