user3606971
user3606971

Reputation: 21

Send Request as JSON param inside JSON object

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": ""
    }
}

Upvotes: 2

Views: 1052

Answers (2)

King of Masses
King of Masses

Reputation: 18775

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"
    }
}

Upvotes: 3

Junaid Hafeez
Junaid Hafeez

Reputation: 1616

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();
        }

Upvotes: 0

Related Questions