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: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:tools.xor.MutableJsonProperty.java

@Override
public void addMapEntry(Object dataObject, Object key, Object value) {
    if (!JSONObject.class.isAssignableFrom(((BusinessObject) dataObject).getInstance().getClass())) {
        throw new IllegalArgumentException("DynamicProperty#addMapEntry dataObject is not of type JSONObject");
    }/*from w  w w. ja  v a 2 s .c  o  m*/

    JSONObject jsonObject = (JSONObject) ((BusinessObject) dataObject).getInstance();
    try {
        jsonObject.put(key.toString(), value);
    } catch (JSONException e) {
        throw ClassUtil.wrapRun(e);
    }
}

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 {/*from w  ww.j  a v a  2 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:com.joyfulmongo.db.ContainerObjectDate.java

@Override
public void onQuery(String collectionName, JSONObject theChild) {
    JSONObject childJson = theChild.getJSONObject(key);

    String iso = childJson.getString("iso");

    try {/*from  w w w .jav  a2  s  .  c o m*/
        Date date = Utils.getParseDateFormat().parse(iso);
        theChild.put(key, date);
    } catch (ParseException e) {
        // do nothing
        LOGGER.log(Level.WARNING, "Wrong format of date string, skip convert.");
    }

}

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

private void messageExecute(String msg) {
    stillProcessing = true;//from  w  w  w.j a  v a 2  s .c om
    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 {//from  w w w  .  j  a  v a  2s .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  w  ww. j  a v a 2s.  c o  m
 *
 * @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();

}

From source file:com.example.m.niceproject.data.Item.java

@Override
public JSONObject toJSON() {
    JSONObject data = new JSONObject();
    try {//from   ww w  .  jav  a2 s.com
        data.put("condition", condition.toJSON());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:com.aipo.container.protocol.AipoProtocolException.java

public AipoProtocolException(AipoErrorCode errorCode) {
    super(errorCode.getStatus(), errorCode.getMessage());
    this.errorCode = errorCode;
    this.response = new JSONObject();
    JSONObject error = new JSONObject();
    String errorMessage = errorCode.getMessage();

    try {//  ww w  . j a va 2s.c  o m
        error.put("message", errorMessage);
        error.put("code", errorCode.getCode());
        response.put("error", error);
    } catch (JSONException e) {
        // ignore
    }
}

From source file:org.wso2.connector.integration.test.base.ConnectorIntegrationTestBase.java

/**
 * Send HTTP request using {@link HttpURLConnection} in JSON format.
 * //from ww w .j a  v  a 2s.  c om
 * @param endPoint String End point URL.
 * @param httpMethod String HTTP method type (GET, POST, PUT etc.)
 * @param headersMap Map<String, String> Headers need to send to the end point.
 * @param requestFileName String File name of the file which contains request body data.
 * @param parametersMap Map<String, String> Additional parameters which is not predefined in the
 *        properties file.
 * @return RestResponse object.
 * @throws JSONException
 * @throws IOException
 */
protected RestResponse<JSONObject> sendJsonRestRequest(String endPoint, String httpMethod,
        Map<String, String> headersMap, String requestFileName, Map<String, String> parametersMap)
        throws IOException, JSONException {

    HttpURLConnection httpConnection = writeRequest(endPoint, httpMethod, RestResponse.JSON_TYPE, headersMap,
            requestFileName, parametersMap);

    String responseString = readResponse(httpConnection);

    RestResponse<JSONObject> restResponse = new RestResponse<JSONObject>();
    restResponse.setHttpStatusCode(httpConnection.getResponseCode());
    restResponse.setHeadersMap(httpConnection.getHeaderFields());

    if (responseString != null) {
        JSONObject jsonObject = null;
        if (isValidJSON(responseString)) {
            jsonObject = new JSONObject(responseString);
        } else {
            jsonObject = new JSONObject();
            jsonObject.put("output", responseString);
        }

        restResponse.setBody(jsonObject);
    }

    return restResponse;
}