Error generating a JSON object

Asked

Viewed 78 times

1

I’m trying to create this object:

{
    "schema": [
        "urn:ietf:params:scim:schemas:core:2.0:User",
        "urn:ietf:params:scim:schemas:core:2.0:User",
        "urn:ietf:params:scim:schemas:core:2.0:User"
    ],
    "endereco": "Rua 0",
    "nome": "Fulano",
    "sobrenome": "de Tal"
}

but he leaves like this:

{
    "{\"schema\":[\"urn:ietf:params:scim:schemas:core:2.0:User\",\"urn:ietf:params:scim:schemas:core:2.0:User\",\"urn:ietf:params:scim:schemas:core:2.0:User\"]}": {
        "endereco": "Rua 0",
        "nome": "Fulano",
        "sobrenome": "de Tal"
    }
}

my code

import org.json.simple.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;


public class Main {

    public static void main(String[] args) throws JSONException {


        JSONArray arr = new JSONArray();
        JSONArray headers = new JSONArray();
        JSONObject usuario = new JSONObject();
        JSONObject head = new JSONObject();
        JSONObject obj = new JSONObject();

        usuario.put("nome", "Fulano");
        usuario.put("sobrenome", "de Tal");
        usuario.put("endereco", "Rua 0");

        headers.put("urn:ietf:params:scim:schemas:core:2.0:User");
        headers.put("urn:ietf:params:scim:schemas:core:2.0:User");
        headers.put("urn:ietf:params:scim:schemas:core:2.0:User");



        head.put("schema", headers);

        obj.put(head, usuario);

        System.out.println(obj);
    }

}

1 answer

1


The logic is incorrect, because you are creating a user object, and adding it together to the head, and the head has no value, just do so

    obj.put("nome", "Fulano");
    obj.put("sobrenome", "de Tal");
    obj.put("endereco", "Rua 0");

Browser other questions tagged

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