Example usage for org.json JSONObject JSONObject

List of usage examples for org.json JSONObject JSONObject

Introduction

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

Prototype

public JSONObject() 

Source Link

Document

Construct an empty JSONObject.

Usage

From source file:old.server.packet.PacketFactory.java

public static Packet make(final ENUM_CLIENT_TYPE client_type, final String username, final String sender_host,
        final int sender_port, final ENUM_PLAYER_ROLE player_role) throws JSONException {
    // Redirect packet
    final JSONObject jo = new JSONObject();

    jo.put(CommonPacketConsts.CLIENT_TYPE, client_type.ordinal());
    jo.put(CommonPacketConsts.ACTION_TYPE, ACTION_REGISTER.ordinal());
    jo.put(CommonPacketConsts.ITEM_USERNAME, username);
    jo.put(CommonPacketConsts.ITEM_HOST, sender_host);
    jo.put(CommonPacketConsts.ITEM_PORT, sender_port);
    jo.put(CommonPacketConsts.PLAYER_ROLE, player_role.ordinal());

    final Packet packet = new Packet(jo.toString());

    return packet;
}

From source file:old.server.packet.PacketFactory.java

public static Packet make(final float ball_x, final float ball_y, final float p1_x, final float p1_y,
        final float p2_x, final float p2_y) throws JSONException {
    // Update game state
    final JSONObject jo = new JSONObject();

    jo.put(CommonPacketConsts.BALL_POS_X, ball_x);
    jo.put(CommonPacketConsts.BALL_POS_Y, ball_y);
    jo.put(CommonPacketConsts.P1_POS_X, p1_x);
    jo.put(CommonPacketConsts.P1_POS_Y, p1_y);
    jo.put(CommonPacketConsts.P2_POS_X, p2_x);
    jo.put(CommonPacketConsts.P2_POS_Y, p2_y);

    final Packet packet = new Packet(jo.toString());

    return packet;
}

From source file:old.server.packet.PacketFactory.java

public static Packet make(final ENUM_CLIENT_TYPE client_type, final String msg) throws JSONException {
    // Update (?) msg
    final JSONObject jo = new JSONObject();

    jo.put(CommonPacketConsts.CLIENT_TYPE, client_type.ordinal());
    jo.put(CommonPacketConsts.ACTION_TYPE, ACTION_UPDATE.ordinal());
    jo.put(CommonPacketConsts.MESSAGE, msg);

    final Packet packet = new Packet(jo.toString());

    return packet;
}

From source file:old.server.packet.PacketFactory.java

public static Packet make(final ENUM_CLIENT_TYPE client_type, final ENUM_MOVE_DIRECTION move_direction)
        throws JSONException {
    // Update direction
    final JSONObject jo = new JSONObject();

    jo.put(CommonPacketConsts.CLIENT_TYPE, client_type.ordinal());
    jo.put(CommonPacketConsts.ACTION_TYPE, ACTION_UPDATE.ordinal());
    jo.put(CommonPacketConsts.MOVE_DIRECTION, move_direction.ordinal());

    final Packet packet = new Packet(jo.toString());

    return packet;
}

From source file:old.server.packet.PacketFactory.java

public static Packet make(final ENUM_CLIENT_TYPE client_type) throws JSONException {
    // Ping packet
    final JSONObject jo = new JSONObject();

    jo.put(CommonPacketConsts.CLIENT_TYPE, client_type.ordinal());
    jo.put(CommonPacketConsts.ACTION_TYPE, ACTION_PING.ordinal());

    final Packet packet = new Packet(jo.toString());

    return packet;
}

From source file:de.jackwhite20.japs.server.command.impl.SetCommand.java

@Override
public boolean execute(String[] args) {

    if (args.length < 2) {
        JaPS.getLogger().info("Usage: set <Key> [Expire] <Value>");
        return false;
    }//  w  ww .  j a  v a 2 s.  co  m

    int expire = -1;
    String value = args[1];

    if (args.length >= 3) {
        try {
            expire = Integer.parseInt(args[1]);
        } catch (NumberFormatException e) {
            JaPS.getLogger().info("'" + args[1] + "' is not a number");
            return false;
        }

        String[] values = new String[args.length - 2];
        System.arraycopy(args, 2, values, 0, args.length - 2);

        value = String.join(" ", values);
    }

    JaPS.getServer().cache().put(args[0], value);

    JaPS.getServer().clusterBroadcast(null, new JSONObject().put("op", OpCode.OP_CACHE_ADD.getCode())
            .put("key", args[0]).put("value", value).put("expire", expire));

    return true;
}

From source file:it.acutus.utilitylibrarycode.parser.XmlToJson.java

private JSONObject convertTagToJson(Tag tag) {
    JSONObject json = new JSONObject();

    // Content is injected as a key/value
    if (tag.getContent() != null) {
        try {/*  www .  j av  a2  s  .c  o m*/
            String path = tag.getPath();
            String name = getContentNameReplacement(path, DEFAULT_CONTENT_NAME);
            json.put(name, tag.getContent());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    try {
        if (tag.isList() || isForcedList(tag)) {
            JSONArray list = new JSONArray();
            ArrayList<Tag> children = tag.getChildren();
            for (Tag child : children) {
                list.put(convertTagToJson(child));
            }
            String childrenNames = tag.getChild(0).getName();
            json.put(childrenNames, list);
            return json;
        } else {
            ArrayList<Tag> children = tag.getChildren();
            if (children.size() == 0) {
                json.put(tag.getName(), tag.getContent());
            } else {
                for (Tag child : children) {
                    if (child.hasChildren()) {
                        JSONObject jsonChild = convertTagToJson(child);
                        json.put(child.getName(), jsonChild);
                    } else {
                        json.put(child.getName(), child.getContent());
                    }
                }
            }
            return json;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.wso2.emm.agent.services.ProcessMessage.java

private void messageExecute(String msg) {
    stillProcessing = true;/*from   w w w. ja  va  2s .c  o m*/
    JSONArray repArray = new JSONArray();
    JSONObject jsReply = null;
    String msgId = "";

    JSONArray dataReply = null;
    try {
        JSONArray jArr = new JSONArray(msg.trim());
        for (int i = 0; i < jArr.length(); i++) {
            JSONArray innerArr = new JSONArray(jArr.getJSONObject(i).getString("data"));
            String featureCode = jArr.getJSONObject(i).getString("code");
            dataReply = new JSONArray();
            jsReply = new JSONObject();
            jsReply.put("code", featureCode);

            for (int x = 0; x < innerArr.length(); x++) {
                msgId = innerArr.getJSONObject(x).getString("messageId");
                jsReply.put("messageId", msgId);

                if (featureCode.equals(CommonUtilities.OPERATION_POLICY_BUNDLE)) {
                    SharedPreferences mainPrefp = context.getSharedPreferences("com.mdm", Context.MODE_PRIVATE);

                    Editor editorp = mainPrefp.edit();
                    editorp.putString("policy", "");
                    editorp.commit();

                    SharedPreferences mainPref = context.getSharedPreferences("com.mdm", Context.MODE_PRIVATE);
                    Editor editor = mainPref.edit();
                    String arrToPut = innerArr.getJSONObject(0).getJSONArray("data").toString();

                    editor.putString("policy", arrToPut);
                    editor.commit();
                }

                String msgData = innerArr.getJSONObject(x).getString("data");
                JSONObject dataObj = new JSONObject("{}");
                operation = new Operation(context);
                if (featureCode.equalsIgnoreCase(CommonUtilities.OPERATION_POLICY_REVOKE)) {
                    operation.operate(featureCode, jsReply);
                    jsReply.put("status", msgId);
                } else {
                    if (msgData.charAt(0) == '[') {
                        JSONArray dataArr = new JSONArray(msgData);
                        for (int a = 0; a < dataArr.length(); a++) {
                            JSONObject innterDataObj = dataArr.getJSONObject(a);
                            featureCode = innterDataObj.getString("code");
                            String dataTemp = innterDataObj.getString("data");
                            if (!dataTemp.isEmpty() && dataTemp != null && !dataTemp.equalsIgnoreCase("null"))
                                dataObj = innterDataObj.getJSONObject("data");

                            dataReply = operation.operate(featureCode, dataObj);
                            //dataReply.put(resultJson);
                        }
                    } else {
                        if (!msgData.isEmpty() && msgData != null && !msgData.equalsIgnoreCase("null"))
                            if (msgData.charAt(0) == '{') {
                                dataObj = new JSONObject(msgData);
                            }
                        dataReply = operation.operate(featureCode, dataObj);
                        //dataReply.put(resultJson);
                    }
                }

            }
            jsReply.put("data", dataReply);
            repArray.put(jsReply);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (Operation.enterpriseWipe == false) {
            SharedPreferences mainPref = context.getSharedPreferences(
                    context.getResources().getString(R.string.shared_pref_package), Context.MODE_PRIVATE);
            String regId = mainPref.getString(context.getResources().getString(R.string.shared_pref_regId), "");
            PayloadParser ps = new PayloadParser();

            replyPayload = ps.generateReply(repArray, regId);
            if (CommonUtilities.DEBUG_MODE_ENABLED) {
                Log.e(TAG, "replyPlayload -" + replyPayload);
            }
            stillProcessing = false;
            getOperations(replyPayload);
        }

    }

}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.ProfilePictureObj.java

public static JSONObject json(byte[] data, boolean reply) {
    String encoded = FastBase64.encodeToString(data);
    JSONObject obj = new JSONObject();
    try {//w  ww  .j a v a2  s  .co  m
        obj.put(DATA, encoded);
        obj.put(REPLY, reply);

    } catch (JSONException e) {
    }
    return obj;
}

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

/**
 * Create JSON request for CreateCase./*from ww  w  .j ava  2s.  c om*/
 *
 * @return JSON payload.
 * @throws JSONException thrown when parsing JSON String.
 */
private String getJsonPayload() throws JSONException {

    JSONObject jsonPayload = new JSONObject();

    String name = (String) messageContext.getProperty(Constants.NAME);
    if (name != null && !name.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.NAME, name);
    }
    String description = (String) messageContext.getProperty(Constants.DESCRIPTION);
    if (description != null && !description.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.DESCRIPTION, description);
    }
    String tags = (String) messageContext.getProperty(Constants.TAGS);
    if (tags != null && !tags.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.TAGS, new JSONArray(tags));
    }
    String leadUser = (String) messageContext.getProperty(Constants.LEAD_USER);
    if (leadUser != null && !leadUser.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.LEAD_USER, leadUser);
    }
    String customer = (String) messageContext.getProperty(Constants.CUSTOMER);
    if (customer != null && !customer.isEmpty()) {
        jsonPayload.put(Constants.JSONKeys.CUSTOMER, customer);
    }
    String customFields = (String) messageContext.getProperty(Constants.CUSTOM_FIELDS);
    if (customFields != null && !customFields.isEmpty()) {

        jsonPayload.put(Constants.JSONKeys.CUSTOM_FIELD, new JSONObject(customFields));
    }

    return jsonPayload.toString();

}