Receiving Json with empty arguments

1

I created an API to receive database data via json for my android application, but some columns in my table are null, generating an empty json argument that is generating error in my application, I would like to find out the best way to treat this information since it is important to know whether or not the variable is empty.

ex:

Json: "redes_sociais":{"id":1,"telefone":"99999","whatsapp":"99999","facebook":"face","instagram":"","twiter":"","youtube":"","email":""}

Construtor da entidade no android:
public RedesItem(JSONObject rede) throws JSONException {
    this.id = rede.getLong("id");
    this.telefone = rede.getString("telefone");
    this.whatsapp = rede.getString("whatsapp");
    this.face = rede.getString("face");
    this.insta = rede.getString("insta");
    this.twiter = rede.getString("twiter");
    this.youtube = rede.getString("youtube");
    this.email = rede.getString("email");
}
    
asked by anonymous 02.04.2018 / 23:10

1 answer

2

Hello,

To check if the key exists in json you can use the has method of the JSONObject class.

See the documentation here

Example:

 this.telefone = (rede.has("telefone")) ? rede.getString("telefone") : null;
    
02.04.2018 / 23:36