Example usage for org.json JSONArray put

List of usage examples for org.json JSONArray put

Introduction

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

Prototype

public JSONArray put(Object value) 

Source Link

Document

Append an object value.

Usage

From source file:org.exoplatform.social.portlet.UISpacesToolBarPortlet.java

protected JSONObject toJSON(UserNode node, String navId, MimeResponse res) throws Exception {
    JSONObject json = new JSONObject();
    String nodeId = node.getId();

    json.put("label", node.getEncodedResolvedLabel());
    json.put("hasChild", node.getChildrenCount() > 0);
    json.put("isSelected", nodeId.equals(getSelectedNode().getId()));
    json.put("icon", node.getIcon());

    ResourceURL rsURL = res.createResourceURL();
    rsURL.setResourceID(res.encodeURL(getResourceIdFromNode(node, navId)));
    json.put("getNodeURL", rsURL.toString());
    json.put("actionLink", Util.getPortalRequestContext().getPortalURI() + node.getURI());

    JSONArray childs = new JSONArray();
    for (UserNode child : node.getChildren()) {
        childs.put(toJSON(child, navId, res));
    }//from  w w  w  . ja  v  a2  s. c  om
    json.put("childs", childs);
    return json;
}

From source file:com.tonikorin.cordova.plugin.LocationProvider.LocationService.java

private void updateLocateHistory(JSONObject messageIn, boolean blocked, String msgType, String time)
        throws JSONException { // Read current history
    SharedPreferences sp = myContext.getSharedPreferences(LocationService.PREFS_NAME, Context.MODE_PRIVATE);
    String historyJsonStr = sp.getString(LocationService.HISTORY_NAME, "{}");
    JSONObject history = new JSONObject(historyJsonStr);
    if (CHAT.equals(msgType)) { // CHAT history, save hole message
        JSONArray chatMessages = history.optJSONArray("chatMessages");
        if (chatMessages == null)
            chatMessages = new JSONArray();
        //Log.d(TAG, "CHAT history TIME: " + time);
        messageIn.put("time", Long.parseLong(time));
        chatMessages.put(messageIn.toString());
        if (chatMessages.length() > 100)
            chatMessages.remove(0); // store only last 100 chat messages
        history.put("chatMessages", chatMessages);
    } else {// Current LOCATE
        String member = messageIn.optString("memberName", "");
        if (blocked)
            member = "\u2717 " + member;
        JSONObject updateStatus = new JSONObject();
        updateStatus.put("member", member);
        updateStatus.put("team", messageIn.optString("teamId", ""));
        updateStatus.put("date", getDateAndTimeString(System.currentTimeMillis()));
        updateStatus.put("target", messageIn.optString("target", ""));
        history.put("updateStatus", updateStatus);
        // History LOCATE lines...
        JSONArray historyLines = history.optJSONArray("lines");
        if (historyLines == null)
            historyLines = new JSONArray();
        String target;//  w  w  w  .  j  a  va  2 s.  c  o  m
        if (updateStatus.getString("target").equals(""))
            target = updateStatus.optString("team");
        else
            target = updateStatus.optString("target");
        String historyLine = updateStatus.getString("member") + " (" + target + ") "
                + updateStatus.getString("date") + "\n";
        historyLines.put(historyLine);
        if (historyLines.length() > 200)
            historyLines.remove(0); // store only last 200 locate queries
        history.put("lines", historyLines);
    }
    // Save new history
    SharedPreferences.Editor editor = sp.edit();
    editor.putString(LocationService.HISTORY_NAME, history.toString());
    editor.commit();
    //Log.d(TAG, "history:" + history.toString());
}

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);
    }//  w  w w .  ja  va 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
 *///from w w  w .ja  v a2  s  . 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
 * /*  w ww  .j ava  2  s  .co  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)./*  www  . j a  v  a  2s. c om*/
 * 
 * @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.
 * //w  ww.jav a 2s . co  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);
        json.put("choices", choicesJson);
    }/*from   w ww  .j  av  a  2 s.c  o  m*/
    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)));
        }/*  w ww  .  j a va  2 s .com*/
    }

    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();
    /*/*from w ww .  j a v  a  2 s .c  o 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;
}