【问题标题】:JAVA/API how to get the correct JSON formatJAVA/API 如何获取正确的 JSON 格式
【发布时间】:2020-08-24 17:04:38
【问题描述】:

下午好,我的问题对某些人来说可能很简单,但我正在学习,我不能得到一个好的结果。

我有一个 API 需要获取具有以下结构的 json 对象:

{
"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
    }]
}
}

但是,当我创建我的 JSON 对象时:

Registro = new mRegistro(userList, personList, asociadoList);

    Gson gson = new Gson();
    json = gson.toJson(Registro);

创建我的结构如下

{
"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
    }]
}]
}

我想要相同的结构,但在 User、Person 和 Associate 参数中我不希望它有方括号,我知道它在那里是因为它是一个列表,它是这样的,因为我的类“mRegistro”包含“usuario、persona 和 asociado”类

添加对象的代码:

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

【问题讨论】:

  • 看来问题出在json = gson.toJson()。您可能需要考虑除 Gson 之外的其他库。我会看Jackson。否则,试试 Gson Custom Serialization: howtodoinjava.com/gson/custom-serialization-deserialization
  • 但是如果你想要前面提到的 Json 结构并且当它的子对象不需要是列表时,你为什么要使用 userListpersonListasociadoList?为什么不让它们成为单个对象而不是对象列表?这样你就不会在对象元素周围有那些列表指示符[ ]
  • @LalitFauzdar 正如我所说,我正在学习,我看到将一个对象插入另一个对象的方式就是这样,我不能以另一种方式做到这一点,我将代码附在答案中。
  • 告诉我们mRegistro.java。无论如何,您希望 usuario 作为对象,为什么要使用 List&lt;User&gt; ? (并尽量遵循 Java 编码约定)
  • 当一个对象实际上是一个列表时,为什么要查看它。这就像希望看到蜘蛛侠赤身裸体。

标签: java android json api


【解决方案1】:

查看此代码:

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)

【讨论】:

  • 谢谢你!我明白了,我明白了。你知道如何防止 jsonobject 中的数据按字母顺序排序吗? o 我必须提出另一个问题?
  • 什么时候进行排序?
  • @JoseMarianoGarfelGarcia 如果您能找到解决方案,您仍然可以查看here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 2019-12-27
  • 2016-03-30
  • 2012-02-01
  • 2021-08-28
  • 1970-01-01
  • 2021-12-07
相关资源
最近更新 更多