Objective
Create a JSON with the following structure:
{
"auth": {
"user": "rbz",
"token": "abc123"
}
}
Scenario
Creating the root structure:
JSONObject JOraiz = new JSONObject();
Creating the values user
and token
:
JSONObject JOauth_u = new JSONObject();
JSONObject JOauth_t = new JSONObject();
JOauth_u.put("user", "rbz");
JOauth_t.put("token", "abc123");
Attempt 1
Using .put()
, the value of JOraiz
is overwritten :
JOraiz.put("auth", JOauth_u);
JOraiz.put("auth", JOauth_t);
Output : {"auth":{"token": "abc123"}}
Attempt 2
Using .acumulate()
, it creates an Array :
JOraiz.accumulate("auth", JOauth_u);
JOraiz.accumulate("auth", JOauth_t);
Output : {"auth":[{"user": "rbz"},{"token": "abc123"}]}
Doubt
- How do I have 2 properties within the same JSON object?