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:wsclient.Hitagi.java

public void joinroom(String room) {
    JSONObject json = new JSONObject();

    json.put("type", "joinroom");
    json.put("room", room);
    json.put("count", 30);

    client.sendMessage(json);//from  ww w  .  j  av a  2  s  .  c o m
}

From source file:wsclient.Hitagi.java

public void chat(String room, String text) {
    JSONObject json = new JSONObject();

    json.put("type", "chat");
    json.put("room", room);
    json.put("text", text);
    json.put("cl", "000000");

    client.sendMessage(json);/* w  w w .  j  av  a 2s .co  m*/
}

From source file:IoTatWork.AuCapTreeResource.java

/**
 * //from w w  w. j  ava 2 s  .  com
 * @param auCapRid
 * @param depth 
 * @param jsonNodeTree 
 * @return
 */
private void getJsonNodeTree(Vertex auCapNode, JSONObject jsonNodeTree, int depth, boolean toRevoke,
        Object revokedSince) {

    //graph.getRawGraph();
    try {

        OGraphDatabase rawGraph = graph.getRawGraph();
        ODocument doc = rawGraph.load((ORID) auCapNode.getId());
        //System.out.println(doc.getClassName());
        if (doc.getClassName().equals(DataMapper.AUCAP_VERTEX)) {
            getJsonAuCapVertexData(auCapNode, jsonNodeTree);
            JSONObject jsonData = (JSONObject) jsonNodeTree.get("data");
            if (!toRevoke) {
                //controllo se lui o i figli sono sa revocare
                String status = (String) auCapNode.getProperty(DataMapper.STATUS);
                if (status.equals(Status.REVOKED)) {
                    String revocationScope = (String) auCapNode.getProperty(DataMapper.REVOCATION_SCOPE);
                    if (revocationScope.equals(RevocationScope.ALL)) {
                        jsonData.put(DataMapper.STATUS, Status.REVOKED);
                        jsonData.put(DataMapper.REVOKED_SINCE, auCapNode.getProperty(DataMapper.REVOKED_SINCE));
                        toRevoke = true;
                        revokedSince = auCapNode.getProperty(DataMapper.DESCENDANT_REVOKED_SINCE);
                    } else if (revocationScope.equals(RevocationScope.THIS)) {
                        jsonData.put(DataMapper.STATUS, Status.REVOKED);
                        jsonData.put(DataMapper.REVOKED_SINCE, auCapNode.getProperty(DataMapper.REVOKED_SINCE));
                    } else if (revocationScope.equals(RevocationScope.DESCENDANT)) {
                        jsonData.put(DataMapper.STATUS, Status.VALID);
                        toRevoke = true;
                        revokedSince = auCapNode.getProperty(DataMapper.DESCENDANT_REVOKED_SINCE);
                    }

                }

            } else {
                //metto lo status a Revoked e metto il campo revokedSince
                jsonData.put(DataMapper.REVOKED_SINCE, revokedSince);
                jsonData.put(DataMapper.STATUS, Status.REVOKED);
            }

            Iterable<Edge> dependentEdges = auCapNode.getEdges(Direction.OUT,
                    DataMapper.DEPENDENT_CAPABILIES_LABEL);
            JSONArray jsonArrayDependant = new JSONArray();
            depth++;
            for (Edge depend : dependentEdges) {

                if (depth <= this.depth) {
                    JSONObject jsonDependantNode = new JSONObject();
                    jsonArrayDependant.put(jsonDependantNode);
                    Vertex dependantVertex = depend.getVertex(Direction.IN);
                    getJsonNodeTree(dependantVertex, jsonDependantNode, depth, toRevoke, revokedSince);
                }

            }

            jsonNodeTree.put("children", jsonArrayDependant);

        } else {
            jsonNodeTree.put("id", auCapNode.getId().toString().substring(1));
            jsonNodeTree.put("name", auCapNode.getProperty("name").toString());

            Iterable<Edge> dependentEdges = auCapNode.getEdges(Direction.OUT);

            JSONObject jsonData = new JSONObject();
            String edgeLabel = dependentEdges.iterator().next().getLabel();
            jsonData.put("edgeLabel", edgeLabel);
            jsonNodeTree.put("data", jsonData);

            JSONArray jsonArrayDependant = new JSONArray();
            depth++;
            for (Edge depend : dependentEdges) {

                if (depth <= this.depth) {
                    JSONObject jsonDependantNode = new JSONObject();
                    jsonArrayDependant.put(jsonDependantNode);
                    Vertex dependantVertex = depend.getVertex(Direction.IN);
                    getJsonNodeTree(dependantVertex, jsonDependantNode, depth, false, null);
                }
            }

            jsonNodeTree.put("children", jsonArrayDependant);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

From source file:IoTatWork.AuCapTreeResource.java

/**
 * /*from  w  ww.j a v a  2 s  .c om*/
 * @param auCapNode
 * @param jsonNodeTree 
 * @return
 */
private void getJsonAuCapVertexData(Vertex auCapNode, JSONObject jsonNodeTree) {
    try {

        Vertex detailsVertex = auCapNode.getEdges(Direction.OUT, DataMapper.DETAILS_LABEL).iterator().next()
                .getVertex(Direction.IN);
        jsonNodeTree.put("id", auCapNode.getId().toString().substring(1));
        jsonNodeTree.put("name", detailsVertex.getProperty(DataMapper.SUBJECT_ID).toString());
        JSONObject jsonVertexData = new JSONObject();
        jsonVertexData.put("edgeLabel", DataMapper.DEPENDENT_CAPABILIES_LABEL);

        //AuCapVertex
        jsonVertexData.put(DataMapper.CAPABILITY_ID, auCapNode.getProperty(DataMapper.CAPABILITY_ID));
        jsonVertexData.put(DataMapper.CAPABILITY_ISSUER, auCapNode.getProperty(DataMapper.CAPABILITY_ISSUER));
        jsonVertexData.put(DataMapper.CAPABILITY_ISSUE_TIME,
                auCapNode.getProperty(DataMapper.CAPABILITY_ISSUE_TIME));

        String status = (String) auCapNode.getProperty(DataMapper.STATUS);
        jsonVertexData.put(DataMapper.STATUS, status);

        /*
        if(status.equals(Status.REVOKED)){
           String revocationScope = (String) auCapNode.getProperty(DataMapper.REVOCATION_SCOPE);
           jsonVertexData.put(DataMapper.REVOCATION_SCOPE, revocationScope);
           if(revocationScope.equals(RevocationScope.ALL)){
              jsonVertexData.put(DataMapper.REVOKED_SINCE, auCapNode.getProperty(DataMapper.REVOKED_SINCE));
              jsonVertexData.put(DataMapper.DESCENDANT_REVOKED_SINCE, auCapNode.getProperty(DataMapper.DESCENDANT_REVOKED_SINCE));
           }else if(revocationScope.equals(RevocationScope.THIS)){
              jsonVertexData.put(DataMapper.REVOKED_SINCE, auCapNode.getProperty(DataMapper.REVOKED_SINCE));
           }else if(revocationScope.equals(RevocationScope.DESCENDANT)){
              jsonVertexData.put(DataMapper.DESCENDANT_REVOKED_SINCE, auCapNode.getProperty(DataMapper.DESCENDANT_REVOKED_SINCE));
           }
                   
        }
        */

        jsonVertexData.put(DataMapper.VALIDITY_START_TIME,
                auCapNode.getProperty(DataMapper.VALIDITY_START_TIME));
        jsonVertexData.put(DataMapper.VALIDITY_END_TIME, auCapNode.getProperty(DataMapper.VALIDITY_END_TIME));

        //AuCapDetails
        jsonVertexData.put(DataMapper.SUBJECT_ID, detailsVertex.getProperty(DataMapper.SUBJECT_ID));
        jsonVertexData.put(DataMapper.RESOURCE_ID, detailsVertex.getProperty(DataMapper.RESOURCE_ID));
        jsonVertexData.put(DataMapper.REVOCATION_SERVICE_URLS,
                detailsVertex.getProperty(DataMapper.REVOCATION_SERVICE_URLS));

        if (!auCapNode.getEdges(Direction.OUT, DataMapper.DEPENDENT_CAPABILIES_LABEL).iterator().hasNext()) {
            jsonVertexData.put("leaf", "leaf");
        }
        //         for(String prop : auCapNode.getPropertyKeys()){
        //            jsonVertexData.put(prop, auCapNode.getProperty(prop));
        //         }
        //         for(String prop : detailsVertex.getPropertyKeys()){
        //            if(!prop.equals(DataMapper.CAPABILITY_TOKEN))
        //               jsonVertexData.put(prop, detailsVertex.getProperty(prop));
        //         }
        JSONArray jsonArrayAccessRights = new JSONArray();
        for (Edge accessRightEdge : detailsVertex.getEdges(Direction.OUT, DataMapper.ACCESS_RIGHTS_LABEL)) {
            Vertex accesRightVertex = accessRightEdge.getVertex(Direction.IN);
            JSONObject jsonAccessRight = new JSONObject();
            for (String prop : accesRightVertex.getPropertyKeys()) {
                jsonAccessRight.put(prop, accesRightVertex.getProperty(prop));
            }
            jsonArrayAccessRights.put(jsonAccessRight);
        }
        jsonVertexData.put(DataMapper.ACCESS_RIGHTS_LABEL, jsonArrayAccessRights);
        jsonNodeTree.put("data", jsonVertexData);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.asd.littleprincesbeauty.data.Task.java

@Override
public JSONObject getCreateAction(int actionId) {
    JSONObject js = new JSONObject();

    try {// w ww  . j  a va2 s  . c  om
        // action_type
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);

        // action_id
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);

        // index
        js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this));

        // entity_delta
        JSONObject entity = new JSONObject();
        entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
        entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null");
        entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE, GTaskStringUtils.GTASK_JSON_TYPE_TASK);
        if (getNotes() != null) {
            entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes());
        }
        js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);

        // parent_id
        js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid());

        // dest_parent_type
        js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE, GTaskStringUtils.GTASK_JSON_TYPE_GROUP);

        // list_id
        js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid());

        // prior_sibling_id
        if (mPriorSibling != null) {
            js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid());
        }

    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("fail to generate task-create jsonobject");
    }

    return js;
}

From source file:com.asd.littleprincesbeauty.data.Task.java

@Override
public JSONObject getUpdateAction(int actionId) {
    JSONObject js = new JSONObject();

    try {/*from   w  w w.j  av  a  2s  . c  om*/
        // action_type
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);

        // action_id
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);

        // id
        js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());

        // entity_delta
        JSONObject entity = new JSONObject();
        entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
        if (getNotes() != null) {
            entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes());
        }
        entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted());
        js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);

    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("fail to generate task-update jsonobject");
    }

    return js;
}

From source file:com.asd.littleprincesbeauty.data.Task.java

@Override
public JSONObject getLocalJSONFromContent() {
    String name = getName();/*from  w ww .  j a v a 2 s  .  c  om*/
    try {
        if (mMetaInfo == null) {
            // new task created from web
            if (name == null) {
                Log.w(TAG, "the note seems to be an empty one");
                return null;
            }

            JSONObject js = new JSONObject();
            JSONObject note = new JSONObject();
            JSONArray dataArray = new JSONArray();
            JSONObject data = new JSONObject();
            data.put(DataColumns.CONTENT, name);
            dataArray.put(data);
            js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
            note.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
            js.put(GTaskStringUtils.META_HEAD_NOTE, note);
            return js;
        } else {
            // synced task
            JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
            JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA);

            for (int i = 0; i < dataArray.length(); i++) {
                JSONObject data = dataArray.getJSONObject(i);
                if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) {
                    data.put(DataColumns.CONTENT, getName());
                    break;
                }
            }

            note.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
            return mMetaInfo;
        }
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        return null;
    }
}

From source file:org.fabrican.extension.variable.provider.servlet.RequestsServlet.java

private void serviceRequest(String n, HttpServletRequest req, HttpServletResponse resp) throws Exception {
    String pKey = req.getParameter(PARAM_PRIMARY);
    String sKey = req.getParameter(PARAM_SECONDARY);
    RuleSet rs = RuleSetDAO.getInstance().getRuleSetByName(n);
    if (rs == null) {
        resp.setStatus(SC_NOT_FOUND);//ww w  . j  a  v a  2  s . c  o  m
        return;
    } else {
        HashMap<String, String> hm = rs.evaluate(pKey, sKey);
        JSONObject jo = new JSONObject();
        for (Entry<String, String> e : hm.entrySet()) {
            jo.put(e.getKey(), e.getValue());
        }
        resp.setStatus(SC_OK);
        resp.getOutputStream().write(jo.toString().getBytes("utf-8"));
    }
}

From source file:cz.karry.vpnc.LunaService.java

/**
 * luna-send -t 1 luna://cz.karry.vpnc/random "{}"
 * /*from  w w w.j  ava 2  s. com*/
 * @param msg
 * @throws JSONException
 * @throws LSException
 */
@LunaServiceThread.PublicMethod
public void random(ServiceMessage msg) throws JSONException, LSException {
    JSONObject reply = new JSONObject();
    reply.put("returnValue", "" + Math.random());
    msg.respond(reply.toString());
}

From source file:cz.karry.vpnc.LunaService.java

/**
 * sudo ip route add 192.168.100.0/24 via 192.168.100.1
 * sudo ip route add default via 192.168.100.1
 *//*from   w w  w . j  av a  2 s.  c o  m*/
@LunaServiceThread.PublicMethod
public void addRoute(ServiceMessage msg) throws JSONException, LSException {

    if ((!msg.getJSONPayload().has("network")) || (!msg.getJSONPayload().has("gateway"))) {
        msg.respondError("1", "Improperly formatted request.");
        return;
    }
    String network = msg.getJSONPayload().getString("network").toLowerCase();
    String gateway = msg.getJSONPayload().getString("gateway").toLowerCase();

    if (!gateway.matches(GATEWAY_REGEXP)) {
        msg.respondError("2", "Bad gateway format.");
        return;
    }
    if (!network.matches(NETWOK_REGEXP)) {
        msg.respondError("3", "Bad network format.");
        return;
    }

    String cmdStr = String.format("ip route add %s via %s", network, gateway);
    CommandLine cmd = new CommandLine(cmdStr);
    if (!cmd.doCmd()) {
        msg.respondError("4", cmd.getResponse());
        return;
    }
    JSONObject reply = new JSONObject();
    reply.put("command", cmdStr);
    msg.respond(reply.toString());
}