Jsonobject with multiple values without using Array

Asked

Viewed 193 times

4

Goal

Create a JSON with the following structure:

{
    "auth": {
        "user": "rbz",
        "token": "abc123"
    }
}

Setting

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 superscript:

JOraiz.put("auth", JOauth_u);
JOraiz.put("auth", JOauth_t);

Exit: {"auth":{"token": "abc123"}}

Attempt 2

Using .acumulate(), he creates a Array:

JOraiz.accumulate("auth", JOauth_u);
JOraiz.accumulate("auth", JOauth_t);

Exit: {"auth":[{"user": "rbz"},{"token": "abc123"}]}


Doubt

  • How do I have 2 properties within the same JSON object?

2 answers

4

I did, in response:

// Criando objeto JSON raiz
JSONObject JOraiz = new JSONObject();

// Criando objeto JSON auth
JSONObject JOauth = new JSONObject();

// Adicionando propriedades e valores ao objeto JOauth 
JOauth.put("user", "rbz");
JOauth.put("token", "abc123");

// Adicionando propriedades e valores ao objeto JOraiz
JOraiz.put("auth", JOauth);

3


If you refer to JSON syntax will see that { and } delimit a Object, which is nothing more than a set of key pairs/value.

Now, analyzing the JSON you want to create:

{
    "auth": {
        "user": "rbz",
        "token": "abc123"
    }
}

He is a Object, because it is bounded by { and }. And it holds a key auth, whose value is:

{
    "user": "rbz",
    "token": "abc123"
}

That is, the value of the key auth is another Object (as it is also bounded by { and }), which in turn has 2 key/value pairs:

  • key user, worthwhile rbz
  • key token, worthwhile abc123

With this, it is easier to know how to create this structure. First I start with the Object more internal - which has 2 key/value pairs (user and token):

// criar o object interno
JSONObject objectInterno = new JSONObject();
// adicionar os 2 pares chave/valor
objectInterno.put("user", "rbz");
objectInterno.put("token", "abc123");

And then we created the Object main, adding the Object internal as key value auth:

// criar o object principal
JSONObject principal = new JSONObject();
// adicionar object interno na chave "auth"
principal.put("auth", objectInterno);

System.out.println(principal.toString(4));

So we have the desired structure:

{"auth": {
    "user": "rbz",
    "token": "abc123"
}}

Your first attempt didn’t work because put overwrites the value if it already exists.

And the second attempt doesn’t work because accumulate places the accumulated values in a array. And besides, you created two Objects different (one for the user, and another to the token), which is not necessary, since both values are in the same Object.

  • 1

    Very good! It was what I needed to have done in my answer, but you’ve done even better! I believe the topic will help a lot who searches on JSONObject!

  • 1

    @Rbz That’s right, many JSON errors occur because we don’t pay attention to the structure and already come out writing code, and a few minutes to analyze before can save us a lot of time :-)

  • 1

    Exactly. If you’re a guy in a hurry, you’re already on the prowl with JSONArray, then to treat is using 3 levels without need. Is that my "TOC" does not let me do gambiarra like this! kkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.