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

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

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:

{
&quot;usuario&quot;: {
    &quot;ID_USUARIO&quot;: 0,
    &quot;CORREO&quot;: &quot;maaridano12345@CORREO.com&quot;,

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

},
&quot;asociado&quot;: {
    &quot;FOTO&quot;: &quot;&quot;,
    &quot;ID_USUARIO&quot;: 0,
    &quot;NOMBRE_EMPRESA&quot;: &quot;EMPRESAMARIANO2&quot;,
    &quot;tipoPagos&quot; : [{
    &quot;ID_TIPO_PAGO&quot; : 1
    }],
    &quot;SERVICIOS&quot; : [{
    &quot;ID_SERVICIO&quot; : 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

{
&quot;usuario&quot;: [{
    &quot;ID_USUARIO&quot;: 0,
    &quot;CORREO&quot;: &quot;maaridano12345@CORREO.com&quot;,

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

}],
&quot;asociado&quot;: [{
    &quot;FOTO&quot;: &quot;&quot;,
    &quot;ID_USUARIO&quot;: 0,
    &quot;NOMBRE_EMPRESA&quot;: &quot;EMPRESAMARIANO2&quot;,
    &quot;tipoPagos&quot; : [{
    &quot;ID_TIPO_PAGO&quot; : 1
    }],
    &quot;SERVICIOS&quot; : [{
    &quot;ID_SERVICIO&quot; : 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&lt;Adress&gt; Domicilios = new ArrayList&lt;Adress&gt;();
    domicilio = new Adress(&quot;MONARCAS&quot;,&quot;CALLETEST&quot;,34,12,83550,1,0,2);
    Domicilios.add(domicilio);

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

    ArrayList&lt;Services&gt; Servicios = new ArrayList&lt;Services&gt;();
    tipo_servicio = new Services(2);
    Servicios.add(tipo_servicio);
    ArrayList&lt;PaymentType&gt; TipoPago = new ArrayList&lt;PaymentType&gt;();
    tipo_pago = new PaymentType(2);
    TipoPago.add(tipo_pago);
    asociado = new Associate(&quot;&quot;,0,&quot;LaEmpresaChida&quot;,0,0,&quot;La mejor empresa&quot;,2, Servicios, TipoPago );

    ArrayList&lt;mRegistro&gt; RegistroAsociado = new ArrayList&lt;mRegistro&gt;();
    ArrayList&lt;Associate&gt; asociadoList = new ArrayList&lt;Associate&gt;();
    asociadoList.add(asociado);
    ArrayList&lt;Person&gt; personList = new ArrayList&lt;Person&gt;();
    personList.add(persona);
    ArrayList&lt;User&gt; userList = new ArrayList&lt;User&gt;();
    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&lt;Associate&gt; asociadoList = new ArrayList&lt;Associate&gt;();
asociadoList.add(asociado);

ArrayList&lt;Person&gt; personList = new ArrayList&lt;Person&gt;();
personList.add(persona);

ArrayList&lt;User&gt; userList = new ArrayList&lt;User&gt;();
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&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:

确定