0

Good afternoon, my question may be very simple for some, but I am learning and I cannot get a good result.

I have an API which requires obtaining a json object with the following structure:

{
"usuario": {
    "ID_USUARIO": 0,
    "CORREO": "maaridano12345@CORREO.com",

    
},
"persona": {
    "ID_USUARIO": 0,
    "NOMBRE_COMPLETO": "MaARIANO2 GARFEL",
    "domicilios" : [{
            "COLONIA" : "CaOLONIA23",
            "CALLE" : "SaTREET",
    }]

},
"asociado": {
    "FOTO": "",
    "ID_USUARIO": 0,
    "NOMBRE_EMPRESA": "EMPRESAMARIANO2",
    "tipoPagos" : [{
    "ID_TIPO_PAGO" : 1
    }],
    "SERVICIOS" : [{
    "ID_SERVICIO" : 2
    }]
}
}

but, when I create my JSON object:

Registro = new mRegistro(userList, personList, asociadoList);

    Gson gson = new Gson();
    json = gson.toJson(Registro);

the structure that creates me is the following

{
"usuario": [{
    "ID_USUARIO": 0,
    "CORREO": "maaridano12345@CORREO.com",

    
}],
"persona": [{
    "ID_USUARIO": 0,
    "NOMBRE_COMPLETO": "MaARIANO2 GARFEL",
    "domicilios" : [{
            "COLONIA" : "CaOLONIA23",
            "CALLE" : "SaTREET",
    }]

}],
"asociado": [{
    "FOTO": "",
    "ID_USUARIO": 0,
    "NOMBRE_EMPRESA": "EMPRESAMARIANO2",
    "tipoPagos" : [{
    "ID_TIPO_PAGO" : 1
    }],
    "SERVICIOS" : [{
    "ID_SERVICIO" : 2
    }]
}]
}

I want the same structure but in the User, Person and Associate parameters I do not want it to have the square bracket, I know that it is there because it is a list, it is like this because my class "mRegistro" contains the classes "usuario, persona and asociado"

Code to add Objects:

usuario = new User(0,emailInput,passwordInput,nameInput,false,false,true,false);

    ArrayList<Adress> Domicilios = new ArrayList<Adress>();
    domicilio = new Adress("MONARCAS","CALLETEST",34,12,83550,1,0,2);
    Domicilios.add(domicilio);

    persona = new Person(0, "MARIANO GARFEL","MARIANO","GARFEL","GARCIA", "9876347586","8575757575",
            "GARFSONAL@GMAIL.COM","CORREO2@FDS.COM", Domicilios);

    ArrayList<Services> Servicios = new ArrayList<Services>();
    tipo_servicio = new Services(2);
    Servicios.add(tipo_servicio);
    ArrayList<PaymentType> TipoPago = new ArrayList<PaymentType>();
    tipo_pago = new PaymentType(2);
    TipoPago.add(tipo_pago);
    asociado = new Associate("",0,"LaEmpresaChida",0,0,"La mejor empresa",2, Servicios, TipoPago );

    ArrayList<mRegistro> RegistroAsociado = new ArrayList<mRegistro>();
    ArrayList<Associate> asociadoList = new ArrayList<Associate>();
    asociadoList.add(asociado);
    ArrayList<Person> personList = new ArrayList<Person>();
    personList.add(persona);
    ArrayList<User> userList = new ArrayList<User>();
    userList.add(usuario);
    Registro = new mRegistro(userList, personList, asociadoList);
Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
  • It looks like the problem is in `json = gson.toJson()`. You might want to consider a different library besides Gson. I'd look at [Jackson](https://www.baeldung.com/jackson). Otherwise, try Gson [Custom Serialization](http://websystique.com/java/json/gson-custom-serialization-example/): https://howtodoinjava.com/gson/custom-serialization-deserialization/ – paulsm4 Aug 24 '20 at 17:12
  • But if you want the aforementioned Json stucture and when its child objects need not be lists then why are you using `userList`, `personList` & `asociadoList`? Why not make them a single object instead of list of object? That way you won't have those list indicators `[ ]` around the object element. – Lalit Fauzdar Aug 24 '20 at 17:32
  • @LalitFauzdar as I said, I am learning and the way I saw to insert an object inside another is that way, I couldn't do it in another way, I attach the code in an answer. – Jose Mariano Garfel Garcia Aug 24 '20 at 17:55
  • Show us `mRegistro.java`. Anyway, you expect `usuario` as object, why do you use a `List` ? (and try to follow Java coding conventions) – PeterMmm Aug 24 '20 at 18:08
  • Why would you want to see an object when it is actually a list. It is like wishing to see spider-man naked. – saurabh gupta Aug 24 '20 at 18:15

1 Answers1

0

See this code:

ArrayList<Associate> asociadoList = new ArrayList<Associate>();
asociadoList.add(asociado);

ArrayList<Person> personList = new ArrayList<Person>();
personList.add(persona);

ArrayList<User> userList = new ArrayList<User>();
userList.add(usuario);

Registro = new mRegistro(userList, personList, asociadoList);

You're creating unnecessary ArrayLists here because of which you're getting list object in your Json output.

What you've to do here is change above code as:

Registro = new mRegistro(usuario, persona, asociado);

But, this will also give an error as the Class mRegistro needs ArrayLists as parameters so change its constructor's arguments from

mRegistro(ArrayList<User> userList, ArrayList<Person> personList, ArrayList<Associate> asociadoList)

to mRegistro(User user, Person person, Associate asociado).

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50