Receive Json with empty arguments

Asked

Viewed 303 times

1

I created an API to receive database data via json for my android application, but some columns of my table get null, generating an empty json argument that is generating error in my application, would like to find out how best 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");
}

1 answer

2


Hello,

To check if there is a key in json you can use the method has of the class Jsonobject.

View documentation here

Example:

 this.telefone = (rede.has("telefone")) ? rede.getString("telefone") : null;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.