英文:
JAVA/API how to get the correct JSON format
问题
{
"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
}
]
}
}
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);
英文:
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);
答案1
得分: 0
看一下这段代码:
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);
你在这里创建了不必要的 ArrayLists
,这就是为什么在你的 Json
输出中会得到列表对象。
你需要做的是将上面的代码更改为:
Registro = new mRegistro(usuario, persona, asociado);
但是,这样做也会导致错误,因为 mRegistro
类需要 ArrayLists
作为参数,所以将其构造函数的参数从
mRegistro(ArrayList<User> userList, ArrayList<Person> personList, ArrayList<Associate> asociadoList)
更改为
mRegistro(User user, Person person, Associate asociado)
英文:
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)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论