0

I want to send Json Parameters (below) in Android - POST Method.

{"message":"This is venkatesh","visit":[5,1,2]}

I tried the below code

                String IDs="5,1,2";
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("message", "This is venkatesh");

                JSONArray jsonArray = new JSONArray();
                jsonArray.put(IDs);
                jsonObject.put("visit", jsonArray);
                String json = jsonObject.toString();
                Log.d("Mainactivity", " json" + json);

I am getting the output is

{"message":"This is venkatesh","visit":["5,1,2"]}
 // Output i am get with double quotes inside visit


{"message":"This is venkatesh","visit":[5,1,2]}
// I want to send this parameter without Double quotes inside the Visit
Venkatesh Selvam
  • 1,382
  • 2
  • 15
  • 28

6 Answers6

3

In array add it as integer not as a String

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("message", "This is venkatesh");
            JSONArray jsonArray = new JSONArray();
            jsonArray.put(5);
            jsonArray.put(1);
            jsonArray.put(2);
            jsonObject.put("visit", jsonArray);
            String json = jsonObject.toString();
            Log.i("TAG", " json" + json); //{"message":"This is venkatesh","visit":[5,1,2]}

        } catch (JSONException e) {
            e.printStackTrace();
        }
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
3
String IDs="5,1,2";
String[] numbers = IDs.split(",");
                JSONArray jsonArray = new JSONArray();

for(int i = 0; i < numbers.length(); i++)
{
    jsonArray.put(Integer.parseInt(numbers[i]));
}

Hope this helps.

Undo
  • 25,519
  • 37
  • 106
  • 129
Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
2

Just replace following line:

jsonArray.put(IDs);

with following code:

jsonArray.put(5);
jsonArray.put(1);
jsonArray.put(2);

So you should use 'int' values if you want to see array without quotes. The point is 'quotes' means that this is String object. Proof is following line of your code:

String IDs="5,1,2";

Johnny Cosmic
  • 516
  • 5
  • 8
0
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh"); 
JSONArray jsonArray = new JSONArray(new int[](5, 1, 2)); 
jsonObject.put("visit", jsonArray);
SoreDakeNoKoto
  • 1,175
  • 1
  • 9
  • 16
0

I am assuming you will convert the String to integer array and after you do this is how you can add,

Well the only difference you need to understand is that, JSON adds double quotes for values of String and not of Integer.

so for Key value pair for String it would be

"key":"value"

so for Key value pair for Integer it would be

"key":123

so for Key value pair for boolean it would be

"key":true

With that knowledge you can edit your code.

Code

    try {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", "This is venkatesh");
        JSONArray jsonArray = new JSONArray();
        jsonArray.put(0,5);
        jsonArray.put(1,1);
        jsonArray.put(2,2);
        jsonObject.put("visit", jsonArray);

        Log.d("TAG","result "+jsonObject.toString());


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

Output

{"message":"This is venkatesh","visit":[5,1,2]}
Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
0
int[] arrayOfInteger=[1,2,3];

JSONObject jsonObject =new JSONObject();
jsonObject .put("message","your message");

JSONArray jsonArray = new JSONArray(arrayOfInteger);
jsonObject .put("visit",jsonArray );

Result : {"message":"your message","visit":[1,2,3]}

DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
Nil
  • 195
  • 3
  • 8