下午好,对于某些人来说,我的问题可能很简单,但是我正在学习中,无法获得良好的结果。
我有一个API,需要获取具有以下结构的json对象:
{
"usuario": {
"ID_USUARIO": 0,
"CORREO": "[email protected]",
},
"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
}]
}
}
但是,当我创建JSON对象时:
Registro = new mRegistro(userList, personList, asociadoList);
Gson gson = new Gson();
json = gson.toJson(Registro);
创造我的结构如下
{
"usuario": [{
"ID_USUARIO": 0,
"CORREO": "[email protected]",
}],
"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
}]
}]
}
我想要相同的结构,但是在User,Person和Associate参数中,我不希望它带有方括号,我知道它在那里是因为它是一个列表,这是因为我的类“ mRegistro”包含这些类“ usuario,角色和关联”
添加对象的代码:
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",
"[email protected]","[email protected]", 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);
参见以下代码:
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);
但是,这也会产生错误,因为Class
mRegistro
需要将ArrayLists作为参数,因此更改其构造函数的参数为
mRegistro(ArrayList<User> userList, ArrayList<Person> personList, ArrayList<Associate> asociadoList)
到mRegistro(User user, Person person, Associate asociado)
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句