sunil
sunil

Reputation: 842

how to combine integer and string into JSON object android

I am trying to make Json object using this string value ''133 Ph\u00f9ng H\u01b0ng, C\u1eeda \u0110\u00f4ng, Ho\u00e0n Ki\u1ebfm, H\u00e0 N\u1ed9i, Vietnam''

But i am getting below error

org.json.JSONException: Value 133 of type java.lang.Integer cannot be converted to JSONObject

This is what I am using to convert to JsonObject

JSONObject jsons = new JSONObject(unicodeString.toString());

Please give any suggestions.

Upvotes: 0

Views: 668

Answers (2)

jazzbpn
jazzbpn

Reputation: 7368

Step 1: Initialize YOUR_Data
Step 2: Create JSONObject
Step 3: Put data to created JSONObject

    /**
     * First Initialize YOUR_DATA
     * */
    String stringValue = "your_string";
    int integerValue = 1;
    boolean booleanValue = true;
    double doubleValue = 1.22;
    long longValue = 1111111111;
    Object objectValue = new Object();

    /**
     * Create JSONObject
     * */
    JSONObject jsonObject = new JSONObject();
    try {
        /**
         * Put your data in JSONObject
         * */
        jsonObject.put("string_key", stringValue);
        jsonObject.put("integer_key", integerValue);
        jsonObject.put("boolean_key", booleanValue);
        jsonObject.put("double_key", doubleValue);
        jsonObject.put("long_key", longValue);
        jsonObject.put("object_key", objectValue);
    } catch (JSONException e) {
        e.printStackTrace();
    }

Upvotes: 0

user6602265
user6602265

Reputation:

Try this way:

 final JSONObject obj = new JSONObject();
     obj.put("add_device",String or integer);
     obj.put("group_description",String or integer);
     obj.put("add_group",String or integer);
     System.out.println("Formed String is-->"+obj);

Upvotes: 1

Related Questions