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() 

Source Link

Document

Construct an empty JSONArray.

Usage

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

public TestRailResponse addResultsForCases(int runId, Results results) throws IOException, TestRailException {
    JSONArray a = new JSONArray();
    for (int i = 0; i < results.getResults().size(); i++) {
        JSONObject o = new JSONObject();
        Result r = results.getResults().get(i);
        o.put("case_id", r.getCaseId()).put("status_id", r.getStatusId()).put("comment", r.getComment())
                .put("elapsed", r.getElapsedTimeString());
        a.put(o);/*from w  ww  . j a  v a 2  s.c o m*/
    }

    String payload = new JSONObject().put("results", a).toString();
    log(payload);
    TestRailResponse response = httpPost("index.php?/api/v2/add_results_for_cases/" + runId, payload);
    return response;
}

From source file:com.remobile.file.FileUtils.java

/**
 * Requests a filesystem in which to store application data.
 *
 * @return a JSONObject representing the file system
 *///  ww w .j  a va2s . c o  m
private JSONArray requestAllFileSystems() throws IOException, JSONException {
    JSONArray ret = new JSONArray();
    for (Filesystem fs : filesystems) {
        ret.put(fs.getRootEntry());
    }
    return ret;
}

From source file:org.mapsforge.directions.TurnByTurnDescriptionToString.java

/**
 * Generates a GeoJSON String which represents the route
 * /*from  w ww . jav a 2 s. c  o  m*/
 * @return a string containing a GeoJSON representation of the route
 * @throws JSONException
 *             if the construction of the JSON fails
 */
public String toJSONString() throws JSONException {
    JSONObject json = new JSONObject();
    JSONArray jsonfeatures = new JSONArray();
    json.put("type", "FeatureCollection");
    json.put("features", jsonfeatures);
    for (TurnByTurnStreet street : streets) {
        JSONObject jsonstreet = new JSONObject();
        jsonstreet.put("type", "Feature");
        JSONArray streetCoordinatesAsJson = new JSONArray();
        for (int j = 0; j < street.points.size(); j++) {
            GeoCoordinate sc = street.points.elementAt(j);
            streetCoordinatesAsJson.put(new JSONArray().put(sc.getLongitude()).put(sc.getLatitude()));
        }
        jsonstreet.put("geometry",
                new JSONObject().put("type", "LineString").put("coordinates", streetCoordinatesAsJson));
        jsonstreet.put("properties",
                new JSONObject().put("Name", street.name).put("Ref", street.ref).put("Length", street.length)
                        .put("Angle", street.angleFromStreetLastStreet).put("Directions", street.turnByTurnText)
                        .put("Type", street.type));
        jsonfeatures.put(jsonstreet);
    }
    return json.toString(2);
}

From source file:com.ichi2.preferences.StepsPreference.java

/**
 * Convert steps format. For better usability, rounded floats are converted to integers (e.g., 1.0 is converted to
 * 1).//w ww  . j  a v  a 2s.c  o m
 * 
 * @param steps String representation of steps.
 * @return The steps as a JSONArray or null if the steps are not valid.
 */
public static JSONArray convertToJSON(String steps) {
    JSONArray ja = new JSONArray();
    steps = steps.trim();
    if (TextUtils.isEmpty(steps)) {
        return ja;
    }
    try {
        for (String s : steps.split("\\s+")) {
            float f = Float.parseFloat(s);
            // 0 or less is not a valid step.
            if (f <= 0) {
                return null;
            }
            // Use whole numbers if we can (but still allow decimals)
            int i = (int) f;
            if (i == f) {
                ja.put(i);
            } else {
                ja.put(f);
            }
        }
    } catch (NumberFormatException e) {
        return null;
    } catch (JSONException e) {
        // Can't serialize float. Value likely too big/small.
        return null;
    }
    return ja;
}

From source file:org.jabsorb.serializer.FixUp.java

/**
 * Convert this FixUp to a JSONArray for transmission over JSON-RPC. The
 * JSONArray will contain two sub JSONArrays, the first one representing the
 * fixup location and the 2nd one representing the original location.
 * /*from  w ww  . ja  v a2 s . c o m*/
 * @return the FixUp represented as a JSONArray.
 */
public JSONArray toJSONArray() {
    JSONArray json = new JSONArray();

    JSONArray fixup = new JSONArray(fixupLocation);
    JSONArray original = new JSONArray(originalLocation);

    json.put(fixup);
    json.put(original);

    return json;
}

From source file:com.basetechnology.s0.agentserver.field.ChoiceField.java

public JSONObject toJson() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("type", "choice");
    if (symbol.name != null)
        json.put("name", symbol.name);
    if (label != null)
        json.put("label", label);
    if (description != null)
        json.put("description", description);
    if (defaultValue != null)
        json.put("default_value", defaultValue);
    if (choices != null) {
        JSONArray choicesJson = new JSONArray();
        for (String choice : choices)
            choicesJson.put(choice);//  ww  w .  j  a  va 2  s.  c  o  m
        json.put("choices", choicesJson);
    }
    if (nominalWidth != 0)
        json.put("nominal_width", nominalWidth);
    if (compute != null)
        json.put("compute", compute);
    return json;
}

From source file:org.zaizi.alfresco.rating.controller.JsonNodeRefUtil.java

private static JSONArray getItems(List<NodeRef> nodeRefs) throws JSONException {
    JSONArray items = new JSONArray();
    for (NodeRef nodeRef : nodeRefs) {
        if (nodeRef != null) {
            items.put(getItem(nodeRef, getTemplateNode(nodeRef)));
        }/*from  ww  w  .  java2s . co  m*/
    }

    return items;
}

From source file:org.zaizi.alfresco.rating.controller.JsonNodeRefUtil.java

private static JSONObject getItem(NodeRef nodeRef, TemplateNode templateNode) throws JSONException {
    JSONObject item = new JSONObject();
    /*//  ww  w  . jav  a 2 s  .  co  m
            
    */

    FileInfo info = ZaiziAlfrescoServiceUtil.getServiceRegistry().getFileFolderService().getFileInfo(nodeRef);

    item.put("nodeRef", templateNode.getNodeRef().toString());
    item.put("nodeType", templateNode.getTypeShort());
    item.put("type", "document");
    item.put("mimetype", templateNode.getMimetype());
    item.put("isFolder", info.isFolder());
    item.put("isLink", info.isLink());
    item.put("fileName", info.getName());

    String title = (String) ZaiziAlfrescoServiceUtil.getNodeService().getProperty(nodeRef,
            ContentModel.PROP_TITLE);
    String displayName = title == null || "".equals(title) ? info.getName() : title;

    item.put("title", title);
    item.put("displayName", displayName);
    item.put("description", (String) ZaiziAlfrescoServiceUtil.getNodeService().getProperty(nodeRef,
            ContentModel.PROP_DESCRIPTION));
    item.put("author",
            (String) ZaiziAlfrescoServiceUtil.getNodeService().getProperty(nodeRef, ContentModel.PROP_AUTHOR));

    item.put("createdOn", toString(
            (Date) ZaiziAlfrescoServiceUtil.getNodeService().getProperty(nodeRef, ContentModel.PROP_CREATED)));
    String username = (String) ZaiziAlfrescoServiceUtil.getNodeService().getProperty(nodeRef,
            ContentModel.PROP_CREATOR);
    String usernameFriendly;
    if (username == null) {
        username = "";
        usernameFriendly = "";
    } else {
        usernameFriendly = getFriendlyUsername(ZaiziAlfrescoServiceUtil.getPersonService().getPerson(username));
    }
    item.put("createdBy", usernameFriendly);
    item.put("createdByUser", username);

    item.put("modifiedOn", toString(
            (Date) ZaiziAlfrescoServiceUtil.getNodeService().getProperty(nodeRef, ContentModel.PROP_MODIFIED)));
    username = (String) ZaiziAlfrescoServiceUtil.getNodeService().getProperty(nodeRef,
            ContentModel.PROP_MODIFIER);
    if (username == null) {
        username = "";
        usernameFriendly = "";
    } else {
        usernameFriendly = getFriendlyUsername(ZaiziAlfrescoServiceUtil.getPersonService().getPerson(username));

    }

    item.put("modifiedBy", usernameFriendly);
    item.put("modifiedByUser", username);

    username = (String) ZaiziAlfrescoServiceUtil.getNodeService().getProperty(nodeRef,
            ContentModel.PROP_LOCK_OWNER);
    if (username == null) {
        username = "";
        usernameFriendly = "";
    } else {
        usernameFriendly = getFriendlyUsername(ZaiziAlfrescoServiceUtil.getPersonService().getPerson(username));

    }
    item.put("lockedBy", usernameFriendly);
    item.put("lockedByUser", username);

    item.put("size", templateNode.getSize());

    item.put("contentUrl", getContentUrl(templateNode));
    item.put("webdavUrl", templateNode.getWebdavUrl());

    // List<Action> actions = ZaiziAlfrescoServiceUtil.getServiceRegistry().getActionService().getActions(nodeRef);
    item.put("tags", array(ZaiziAlfrescoServiceUtil.getTaggingService().getTags(nodeRef)));

    item.put("rating", getRating(nodeRef));

    try {

        List<TemplateNode> categoriesList = (List<TemplateNode>) templateNode.getProperties().get("categories");

        if (categoriesList != null) {

            JSONArray categories = new JSONArray();
            JSONArray category;
            for (TemplateNode cat : categoriesList) {
                category = new JSONArray();
                category.put(cat.getName());
                category.put(cat.getDisplayPath().replace("/categories/General", ""));
                categories.put(category);
            }
            item.put("categories", categories);
        }

    } catch (Exception e) {
        logger.error(e, e);
    }

    List<WorkflowInstance> activeWorkflows = ZaiziAlfrescoServiceUtil.getServiceRegistry().getWorkflowService()
            .getWorkflowsForContent(nodeRef, true);

    StringBuilder activeWorkflowsSB = new StringBuilder();
    for (int i = 0; i < activeWorkflows.size(); i++) {
        activeWorkflowsSB.append(activeWorkflows.get(i).getId());
        if (i < activeWorkflows.size() - 1) {
            activeWorkflowsSB.append(",");
        }
    }

    item.put("activeWorkflows", activeWorkflowsSB.toString());
    item.put("isFavourite", false);
    item.put("location", getLocation(nodeRef, templateNode));
    item.put("permissions", getPermissions(nodeRef, templateNode));
    item.put("custom", getCustomProperties(nodeRef, templateNode));
    //item.put("actionLabels", null);
    return item;
}

From source file:org.zaizi.alfresco.rating.controller.JsonNodeRefUtil.java

private static JSONArray array(List<String> values) {
    JSONArray array = new JSONArray();
    for (String value : values) {
        array.put(value);/*from w w w . j a va 2s.c o  m*/
    }
    return array;
}

From source file:org.zaizi.alfresco.rating.controller.JsonNodeRefUtil.java

private static JSONArray array(String... values) {
    JSONArray array = new JSONArray();
    for (String value : values) {
        array.put(value);/*from w w w.  j av a 2s . com*/
    }
    return array;
}