2

I want to send my request parameters in JSON format. My problem is I am not able send request like below format. Can anybody help me to resolve it?

{
    "user": {
        "email" : "",
        "password": "",
        "password_confirmation": ""
    }
}
piet.t
  • 11,718
  • 21
  • 43
  • 52

2 Answers2

3

Try this

      try {
            JSONObject parent = new JSONObject();
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("email", "email");
            jsonObject.put("password", "password");
            jsonObject.put("password_confirmation", "password_confirmation");
            parent.put("user", jsonObject);
            Log.d("output", parent.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

It will return this JsonObject output

{
    "user": {
        "email" : "email",
        "password": "password",
        "password_confirmation": "password_confirmation"
    }
}
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
King of Masses
  • 18,405
  • 4
  • 60
  • 77
0

you are probably looking for something like this

try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("email", "value");
            jsonObject.put("password", "value");
            jsonObject.put("password_confirmation", "value");

            JSONObject parent = new JSONObject();
            parent.put("user", jsonObject);
        }catch (JSONException e) {
            e.printStackTrace();
        }
Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25