0

I want to create json request string. How can I do that? This is my following array:

[1,2,3,4,5];

Now I want to create this type of json request:

{
    "contact_approve":[
    {
    "contact_id":1    
    },
    {
    "contact_id":2    
    },
    {
    "contact_id":3    
    },
    {
    "contact_id":4    
    },
    {
    "contact_id":5    
    },
    ]

} 

Does anyone has the idea about how to create this type of json string?

Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
Jatin Devani
  • 41
  • 1
  • 7

5 Answers5

0

Here is code:

  try {
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();
        int[] dataArray = {1,2,3,4,5};
        for( int i=0;i<dataArray.length;i++) {
            JSONObject internalObject = new JSONObject();
            internalObject.put("contact_id",dataArray[i]);
            array.put(internalObject);

        }
        jsonObject.put("contact_approve", array);

        System.out.print(jsonObject);
        Log.v("JsonObject",jsonObject.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

Thanks

Ankit Patidar
  • 2,731
  • 1
  • 14
  • 22
0

Well it's simple you can use Gson library to convert objects to json like below;

Gson gson = new Gson();
String json = gson.toJson({your object you want to convert to json});

Another thing to mention is if you are sending request to server you don't even need to do json serialization on your own, Use Retrofit and it will do this for you.

Umer Khan
  • 21
  • 5
0

Iterate through the array and print the relevant string for each element.

public static String ConvertToContactJson(int [] array)
{
    String finalstr = "{\"contact_approve\":[";

    for (int i=0; i< array.length; i++)
    {
        finalstr += "{\"contact_id\":"+array[i]+"}";

        if (i < array.length-1) finalstr += ",";
    }

    finalstr += "]}";

    return finalstr;
}
Jaywaa
  • 373
  • 1
  • 4
  • 11
0

Try This

try{

JSONArray jarray=new JSonArray();

for(int i=0;i<array.size();i++){

JSONObject j=new JSONObject();
j.put("contact_id",array[i]);

jarray.put(j);

}
}catch(Exception e){

e.printStackStrace();

}
0

JSONObject which start with {} JSONArray which start with [] In your case you have a JSONObject which has an JSONArray.

let me give you an example.

  public void createJson() throws JSONException {
    // Create main JSON object
    JSONObject jsonObject = new JSONObject();

    //Create an JSON ARRAY which will have a key "contact_approve" later on
    JSONArray jsonArray = new JSONArray();

    Integer contacts[] = {1,2,3,4,5};
    for (int i = 0; i < contacts.length ; i++) {
        JSONObject contactID = new JSONObject();
        jsonArray.put(contactID.put("contact_id", i));
    }

    jsonObject.put("contact_approve", jsonArray);

    String json = jsonObject.toString();
    Log.d("JSON", json);
}

Incase if you need to Parse the JSON response, have a look at Moshi and GSON libraries which make your day better. These libraries can Parse JAVA classes in to JSON. I use [https://codebeautify.org/jsonviewer] to view or minify the json.

Good luck.