JAVA/API 如何获得正确的 JSON 格式

huangapple go评论118阅读模式
英文:

JAVA/API how to get the correct JSON format

问题

  1. {
  2. "usuario": {
  3. "ID_USUARIO": 0,
  4. "CORREO": "maaridano12345@CORREO.com"
  5. },
  6. "persona": {
  7. "ID_USUARIO": 0,
  8. "NOMBRE_COMPLETO": "MaARIANO2 GARFEL",
  9. "domicilios": [
  10. {
  11. "COLONIA": "CaOLONIA23",
  12. "CALLE": "SaTREET"
  13. }
  14. ]
  15. },
  16. "asociado": {
  17. "FOTO": "",
  18. "ID_USUARIO": 0,
  19. "NOMBRE_EMPRESA": "EMPRESAMARIANO2",
  20. "tipoPagos": [
  21. {
  22. "ID_TIPO_PAGO": 1
  23. }
  24. ],
  25. "SERVICIOS": [
  26. {
  27. "ID_SERVICIO": 2
  28. }
  29. ]
  30. }
  31. }

Code to add Objects:

  1. usuario = new User(0,emailInput,passwordInput,nameInput,false,false,true,false);
  2. ArrayList<Adress> Domicilios = new ArrayList<Adress>();
  3. domicilio = new Adress("MONARCAS","CALLETEST",34,12,83550,1,0,2);
  4. Domicilios.add(domicilio);
  5. persona = new Person(0, "MARIANO GARFEL","MARIANO","GARFEL","GARCIA", "9876347586","8575757575",
  6. "GARFSONAL@GMAIL.COM","CORREO2@FDS.COM", Domicilios);
  7. ArrayList<Services> Servicios = new ArrayList<Services>();
  8. tipo_servicio = new Services(2);
  9. Servicios.add(tipo_servicio);
  10. ArrayList<PaymentType> TipoPago = new ArrayList<PaymentType>();
  11. tipo_pago = new PaymentType(2);
  12. TipoPago.add(tipo_pago);
  13. asociado = new Associate("",0,"LaEmpresaChida",0,0,"La mejor empresa",2, Servicios, TipoPago );
  14. ArrayList<mRegistro> RegistroAsociado = new ArrayList<mRegistro>();
  15. ArrayList<Associate> asociadoList = new ArrayList<Associate>();
  16. asociadoList.add(asociado);
  17. ArrayList<Person> personList = new ArrayList<Person>();
  18. personList.add(persona);
  19. ArrayList<User> userList = new ArrayList<User>();
  20. userList.add(usuario);
  21. 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:

  1. {
  2. &quot;usuario&quot;: {
  3. &quot;ID_USUARIO&quot;: 0,
  4. &quot;CORREO&quot;: &quot;maaridano12345@CORREO.com&quot;,
  5. },
  6. &quot;persona&quot;: {
  7. &quot;ID_USUARIO&quot;: 0,
  8. &quot;NOMBRE_COMPLETO&quot;: &quot;MaARIANO2 GARFEL&quot;,
  9. &quot;domicilios&quot; : [{
  10. &quot;COLONIA&quot; : &quot;CaOLONIA23&quot;,
  11. &quot;CALLE&quot; : &quot;SaTREET&quot;,
  12. }]
  13. },
  14. &quot;asociado&quot;: {
  15. &quot;FOTO&quot;: &quot;&quot;,
  16. &quot;ID_USUARIO&quot;: 0,
  17. &quot;NOMBRE_EMPRESA&quot;: &quot;EMPRESAMARIANO2&quot;,
  18. &quot;tipoPagos&quot; : [{
  19. &quot;ID_TIPO_PAGO&quot; : 1
  20. }],
  21. &quot;SERVICIOS&quot; : [{
  22. &quot;ID_SERVICIO&quot; : 2
  23. }]
  24. }
  25. }

but, when I create my JSON object:

  1. Registro = new mRegistro(userList, personList, asociadoList);
  2. Gson gson = new Gson();
  3. json = gson.toJson(Registro);

the structure that creates me is the following

  1. {
  2. &quot;usuario&quot;: [{
  3. &quot;ID_USUARIO&quot;: 0,
  4. &quot;CORREO&quot;: &quot;maaridano12345@CORREO.com&quot;,
  5. }],
  6. &quot;persona&quot;: [{
  7. &quot;ID_USUARIO&quot;: 0,
  8. &quot;NOMBRE_COMPLETO&quot;: &quot;MaARIANO2 GARFEL&quot;,
  9. &quot;domicilios&quot; : [{
  10. &quot;COLONIA&quot; : &quot;CaOLONIA23&quot;,
  11. &quot;CALLE&quot; : &quot;SaTREET&quot;,
  12. }]
  13. }],
  14. &quot;asociado&quot;: [{
  15. &quot;FOTO&quot;: &quot;&quot;,
  16. &quot;ID_USUARIO&quot;: 0,
  17. &quot;NOMBRE_EMPRESA&quot;: &quot;EMPRESAMARIANO2&quot;,
  18. &quot;tipoPagos&quot; : [{
  19. &quot;ID_TIPO_PAGO&quot; : 1
  20. }],
  21. &quot;SERVICIOS&quot; : [{
  22. &quot;ID_SERVICIO&quot; : 2
  23. }]
  24. }]
  25. }

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:

  1. usuario = new User(0,emailInput,passwordInput,nameInput,false,false,true,false);
  2. ArrayList&lt;Adress&gt; Domicilios = new ArrayList&lt;Adress&gt;();
  3. domicilio = new Adress(&quot;MONARCAS&quot;,&quot;CALLETEST&quot;,34,12,83550,1,0,2);
  4. Domicilios.add(domicilio);
  5. persona = new Person(0, &quot;MARIANO GARFEL&quot;,&quot;MARIANO&quot;,&quot;GARFEL&quot;,&quot;GARCIA&quot;, &quot;9876347586&quot;,&quot;8575757575&quot;,
  6. &quot;GARFSONAL@GMAIL.COM&quot;,&quot;CORREO2@FDS.COM&quot;, Domicilios);
  7. ArrayList&lt;Services&gt; Servicios = new ArrayList&lt;Services&gt;();
  8. tipo_servicio = new Services(2);
  9. Servicios.add(tipo_servicio);
  10. ArrayList&lt;PaymentType&gt; TipoPago = new ArrayList&lt;PaymentType&gt;();
  11. tipo_pago = new PaymentType(2);
  12. TipoPago.add(tipo_pago);
  13. asociado = new Associate(&quot;&quot;,0,&quot;LaEmpresaChida&quot;,0,0,&quot;La mejor empresa&quot;,2, Servicios, TipoPago );
  14. ArrayList&lt;mRegistro&gt; RegistroAsociado = new ArrayList&lt;mRegistro&gt;();
  15. ArrayList&lt;Associate&gt; asociadoList = new ArrayList&lt;Associate&gt;();
  16. asociadoList.add(asociado);
  17. ArrayList&lt;Person&gt; personList = new ArrayList&lt;Person&gt;();
  18. personList.add(persona);
  19. ArrayList&lt;User&gt; userList = new ArrayList&lt;User&gt;();
  20. userList.add(usuario);
  21. Registro = new mRegistro(userList, personList, asociadoList);

答案1

得分: 0

看一下这段代码:

  1. ArrayList<Associate> asociadoList = new ArrayList<Associate>();
  2. asociadoList.add(asociado);
  3. ArrayList<Person> personList = new ArrayList<Person>();
  4. personList.add(persona);
  5. ArrayList<User> userList = new ArrayList<User>();
  6. userList.add(usuario);
  7. Registro = new mRegistro(userList, personList, asociadoList);

你在这里创建了不必要的 ArrayLists,这就是为什么在你的 Json 输出中会得到列表对象。

你需要做的是将上面的代码更改为:

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

但是,这样做也会导致错误,因为 mRegistro 类需要 ArrayLists 作为参数,所以将其构造函数的参数从

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

更改为

  1. mRegistro(User user, Person person, Associate asociado)
英文:

See this code:

  1. ArrayList&lt;Associate&gt; asociadoList = new ArrayList&lt;Associate&gt;();
  2. asociadoList.add(asociado);
  3. ArrayList&lt;Person&gt; personList = new ArrayList&lt;Person&gt;();
  4. personList.add(persona);
  5. ArrayList&lt;User&gt; userList = new ArrayList&lt;User&gt;();
  6. userList.add(usuario);
  7. 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:

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

  1. mRegistro(ArrayList&lt;User&gt; userList, ArrayList&lt;Person&gt; personList, ArrayList&lt;Associate&gt; asociadoList)

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

huangapple
  • 本文由 发表于 2020年8月25日 01:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63565559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定