简体   繁体   中英

JAVA/API how to get the correct JSON format

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

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM