Example usage for org.json JSONObject put

List of usage examples for org.json JSONObject put

Introduction

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

Prototype

public JSONObject put(String key, Object value) throws JSONException 

Source Link

Document

Put a key/value pair in the JSONObject.

Usage

From source file:com.polyvi.xface.extension.advancedfiletransfer.XFileUploader.java

@Override
public void onProgressUpdated(int completeSize, long totalSize) {
    JSONObject jsonObj = new JSONObject();
    Status status = Status.PROGRESS_CHANGING;
    try {/*from  w w  w .  j a  va 2 s.  c  o  m*/
        jsonObj.put("loaded", completeSize);
        jsonObj.put("total", totalSize);
    } catch (JSONException e) {
        status = Status.JSON_EXCEPTION;
        XLog.e(CLASS_NAME, e.getMessage());
    }
    XExtensionResult result = new XExtensionResult(status, jsonObj);
    result.setKeepCallback(true);
    mCallbackCtx.sendExtensionResult(result);
}

From source file:com.jennifer.ui.chart.grid.DateGrid.java

protected void drawBottom(Transform root) {
    int full_height = chart.area("height");

    boolean hasLine = options.optBoolean("line", false);

    if (!hasLine) {
        JSONObject o = new JSONObject();
        o.put("x2", chart.area("width"));
        root.append(this.axisLine(o));
    }// w  w  w.  j a v  a 2s  .c  o m

    for (int i = 0, len = ticks.length(); i < len; i++) {

        Transform axis = root.group().translate(values.getDouble(i), 0);

        JSONObject lineOpt = new JSONObject();
        lineOpt.put("y2", hasLine ? -full_height : bar);

        axis.append(line(lineOpt));

        JSONObject textOpt = new JSONObject();
        textOpt.put("x", 0);
        textOpt.put("y", bar * 3);
        textOpt.put("text-anchor", "middle");
        textOpt.put("fill", chart.theme("gridFontColor"));

        axis.append(this.getTextRotate(chart.text(textOpt, getFormatString(ticks.getLong(i)))));
    }
}

From source file:com.jennifer.ui.chart.grid.DateGrid.java

protected void drawLeft(Transform root) {
    int full_width = chart.area("width");

    boolean hasLine = options.optBoolean("line", false);

    if (!hasLine) {
        JSONObject o = new JSONObject();
        o.put("y2", chart.area("height"));
        root.append(this.axisLine(o));
    }//from  w ww  .  j ava2 s. com

    for (int i = 0, len = ticks.length(); i < len; i++) {

        Transform axis = root.group().translate(0, values.getDouble(i));

        JSONObject lineOpt = new JSONObject();
        lineOpt.put("x2", hasLine ? full_width : -bar);

        axis.append(line(lineOpt));

        JSONObject textOpt = new JSONObject();
        textOpt.put("x", -bar - 4);
        textOpt.put("y", bar);
        textOpt.put("text-anchor", "end");
        textOpt.put("fill", chart.theme("gridFontColor"));

        axis.append(this.getTextRotate(chart.text(textOpt, getFormatString(ticks.getLong(i)))));
    }
}

From source file:com.jennifer.ui.chart.grid.DateGrid.java

protected void drawRight(Transform root) {
    int full_width = chart.area("width");

    boolean hasLine = options.optBoolean("line", false);

    if (!hasLine) {
        JSONObject o = new JSONObject().put("y2", chart.area("height"));
        root.append(this.axisLine(o));
    }/*from   w w  w.  jav  a  2  s  .c o m*/

    for (int i = 0, len = ticks.length(); i < len; i++) {

        Transform axis = root.group().translate(0, values.getDouble(i));

        JSONObject lineOpt = new JSONObject();
        lineOpt.put("x2", hasLine ? -full_width : bar);

        axis.append(line(lineOpt));

        JSONObject textOpt = new JSONObject();
        textOpt.put("x", bar + 4);
        textOpt.put("y", -bar);
        textOpt.put("text-anchor", "start");
        textOpt.put("fill", chart.theme("gridFontColor"));

        axis.append(this.getTextRotate(chart.text(textOpt, getFormatString(ticks.getLong(i)))));
    }
}

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

private void doUpdate(Query post, HttpServletResponse response) throws IOException {
    String callback = post.get("callback", null);
    boolean jsonp = callback != null && callback.length() > 0;
    String data = post.get("data", "");
    if (data == null || data.length() == 0) {
        response.sendError(400, "your request must contain a data object.");
        return;//from  w ww.  ja va  2 s.  c om
    }

    // parse the json data
    boolean success;

    try {
        JSONObject json = null;
        try {
            json = new JSONObject(data);
        } catch (JSONException e) {
        }
        if (json == null) {
            throw new IOException("cannot parse json");
        }
        if (!json.has("id_str")) {
            throw new IOException("id_str field missing");
        }
        if (!json.has("source_type")) {
            throw new IOException("source_type field missing");
        }
        ImportProfileEntry i = DAO.SearchLocalImportProfiles(json.getString("id_str"));
        if (i == null) {
            throw new IOException("import profile id_str field '" + json.getString("id_str") + "' not found");
        }
        ImportProfileEntry importProfileEntry;
        try {
            importProfileEntry = new ImportProfileEntry(json);
        } catch (IllegalArgumentException e) {
            response.sendError(400, "Error updating import profile : " + e.getMessage());
            return;
        }
        importProfileEntry.setLastModified(new Date());
        success = DAO.writeImportProfile(importProfileEntry, true);
    } catch (IOException | NullPointerException e) {
        response.sendError(400, "submitted data is invalid : " + e.getMessage());
        Log.getLog().warn(e);
        return;
    }

    post.setResponse(response, "application/javascript");
    JSONObject json = new JSONObject(true);
    json.put("status", "ok");
    json.put("records", success ? 1 : 0);
    json.put("message", "updated");
    // write json
    response.setCharacterEncoding("UTF-8");
    PrintWriter sos = response.getWriter();
    if (jsonp)
        sos.print(callback + "(");
    sos.print(json.toString(2));
    if (jsonp)
        sos.println(");");
    sos.println();
    post.finalize();
}

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

private void doDelete(Query post, HttpServletResponse response) throws IOException {

    String callback = post.get("callback", null);
    boolean jsonp = callback != null && callback.length() > 0;

    String id = post.get("id_str", "");
    if ("".equals(id)) {
        response.sendError(400, "your request must contain a `id_str` parameter.");
        return;// www. j  ava 2s .c o m
    }

    String screen_name = post.get("screen_name", "");
    if ("".equals(screen_name)) {
        response.sendError(400, "your request must contain a `screen_name` parameter.");
        return;
    }

    ImportProfileEntry entry = DAO.SearchLocalImportProfiles(id);
    List<String> sharers = entry.getSharers();
    boolean sharerExists = sharers.remove(screen_name);
    entry.setSharers(sharers);
    boolean successful = false;
    if (sharerExists && DAO.writeImportProfile(entry, true)) {
        successful = true;
    } else {
        throw new IOException("Unable to delete import profile : " + entry.getId());
    }
    post.setResponse(response, "application/javascript");
    JSONObject json = new JSONObject(true);
    json.put("status", "ok");
    json.put("records", sharerExists && successful ? 1 : 0);
    json.put("message", "deleted");
    // write json
    response.setCharacterEncoding("UTF-8");
    PrintWriter sos = response.getWriter();
    if (jsonp)
        sos.print(callback + "(");
    sos.print(json.toString());
    if (jsonp)
        sos.println(");");
    sos.println();
}

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

private void doSearch(Query post, HttpServletResponse response) throws IOException {
    String callback = post.get("callback", "");
    boolean minified = post.get("minified", false);
    boolean jsonp = callback != null && callback.length() > 0;
    String source_type = post.get("source_type", "");
    String screen_name = post.get("screen_name", "");
    String msg_id = post.get("msg_id", "");
    String detailed = post.get("detailed", "");
    // source_type either has to be null a a valid SourceType value
    if (!"".equals(source_type) && !SourceType.isValid(source_type)) {
        response.sendError(400, "your request must contain a valid source_type parameter.");
        return;//from   w  w w.j  av  a  2  s .com
    }
    Map<String, String> searchConstraints = new HashMap<>();
    if (!"".equals(source_type)) {
        searchConstraints.put("source_type", source_type);
    }
    if (!"".equals(screen_name)) {
        searchConstraints.put("sharers", screen_name);
    }
    if (!"".equals(msg_id)) {
        searchConstraints.put("imported", msg_id);
    }
    Collection<ImportProfileEntry> entries = DAO.SearchLocalImportProfilesWithConstraints(searchConstraints,
            true);
    JSONArray entries_to_map = new JSONArray();
    for (ImportProfileEntry entry : entries) {
        JSONObject entry_to_map = entry.toJSON();
        if ("true".equals(detailed)) {
            String query = "";
            for (String msgId : entry.getImported()) {
                query += "id:" + msgId + " ";
            }
            DAO.SearchLocalMessages search = new DAO.SearchLocalMessages(query, Timeline.Order.CREATED_AT, 0,
                    1000, 0);
            entry_to_map.put("imported",
                    search.timeline.toJSON(false, "search_metadata", "statuses").get("statuses"));
        }
        entries_to_map.put(entry_to_map);
    }
    post.setResponse(response, "application/javascript");

    JSONObject m = new JSONObject(true);
    JSONObject metadata = new JSONObject();
    metadata.put("count", entries.size());
    metadata.put("client", post.getClientHost());
    m.put("search_metadata", metadata);
    m.put("profiles", entries_to_map);

    // write json
    response.setCharacterEncoding("UTF-8");
    PrintWriter sos = response.getWriter();
    if (jsonp)
        sos.print(callback + "(");
    sos.print(minified ? m.toString() : m.toString(2));
    if (jsonp)
        sos.println(");");
    sos.println();
}

From source file:com.liferay.mobile.android.v62.resourcepermission.ResourcePermissionService.java

public void addResourcePermission(long groupId, long companyId, String name, int scope, String primKey,
        long roleId, String actionId) throws Exception {
    JSONObject _command = new JSONObject();

    try {/*w  w  w .j  a v  a  2 s  .  c om*/
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("companyId", companyId);
        _params.put("name", checkNull(name));
        _params.put("scope", scope);
        _params.put("primKey", checkNull(primKey));
        _params.put("roleId", roleId);
        _params.put("actionId", checkNull(actionId));

        _command.put("/resourcepermission/add-resource-permission", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    session.invoke(_command);
}

From source file:com.liferay.mobile.android.v62.resourcepermission.ResourcePermissionService.java

public void removeResourcePermission(long groupId, long companyId, String name, int scope, String primKey,
        long roleId, String actionId) throws Exception {
    JSONObject _command = new JSONObject();

    try {//www. j a v  a2 s  .com
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("companyId", companyId);
        _params.put("name", checkNull(name));
        _params.put("scope", scope);
        _params.put("primKey", checkNull(primKey));
        _params.put("roleId", roleId);
        _params.put("actionId", checkNull(actionId));

        _command.put("/resourcepermission/remove-resource-permission", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    session.invoke(_command);
}

From source file:com.liferay.mobile.android.v62.resourcepermission.ResourcePermissionService.java

public void removeResourcePermissions(long groupId, long companyId, String name, int scope, long roleId,
        String actionId) throws Exception {
    JSONObject _command = new JSONObject();

    try {/*from   w  w w. ja v a 2s  . co  m*/
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("companyId", companyId);
        _params.put("name", checkNull(name));
        _params.put("scope", scope);
        _params.put("roleId", roleId);
        _params.put("actionId", checkNull(actionId));

        _command.put("/resourcepermission/remove-resource-permissions", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    session.invoke(_command);
}