1

I am fetching a JSON using retrofit 2, which carries object if the key has value else an empty array. ex:

If the key i.e address has values it returns object

{
    "student": {
        "name": "Some name",
        "address": {
            "house": "5",
            "road": "3"
        }
    }
}

If the key i.e address does not have any value it returns empty array

{
    "student": {
        "name": "Some name",
        "address": []
    }
}

In my POJO class I have made my Address class type to object so that retrofit can parse the JSON.

public class Student {

@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private Object address;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Object getAddress() {
return address;
}

public void setAddress(Object address) {
this.address = address;
}

}

Now how can I check that the address type is object or an array?

I have tried with isArray(), but did not find result.

if(obj.getclass().isArray())

Thanks in advance.

MSD
  • 1,409
  • 12
  • 25
Imtiaz Hossain
  • 347
  • 5
  • 16

4 Answers4

0

You can check through instanceof is object is JosnObject or JsonArray?

if (address instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) address;
}
else  if (address instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) address;
}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

You can go for json schema validation where you have to definie everything regrading type and format of json and later on validate your json using Jackson 2.x libraries and fge/json-schema-validator.

Reference link for more details.

http://wilddiary.com/validate-json-against-schema-in-java/

Avyaan
  • 1,285
  • 6
  • 20
  • 47
0

I wonder why your json is not consistent? I mean if there is no value in address it not supposed to be a JSONArray

Instead it supposed to be like -

"address": {
    "house": "",
    "road": ""
}

It will easy to parse if you are doing like above - Now you Pojo will be -

public class Address {

@SerializedName("house")
@Expose
private String house;
@SerializedName("road")
@Expose
private String road;

public String getHouse() {
return house;
}

public void setHouse(String house) {
this.house = house;
}

public String getRoad() {
return road;
}

public void setRoad(String road) {
this.road = road;
}

}

And finally you can get the Address using -

@SerializedName("address")
@Expose
private Address address;

in your Student class

PS,
Server side problems must be fixed by Server side.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26
0

Thanks to all. I have found my answer with the help of instanceof. Instead of checking directly the address obj, I have to convert the value into Object type, i.e

Gson gson = new GsonBuilder().serializeNulls().setLenient().create();
String jsonStr = gson.toJson(obj.getAddress());
Object json = new JSONTokener(jsonStr).nextValue();

And then I can check with JSONObject

if (json instanceof JSONObject) {
 // Object
}else{
 // Array
}
Imtiaz Hossain
  • 347
  • 5
  • 16