How to get how many key inside a gson object model class android?

Viewed 410

I have a response that will look like this:

    "field_1":{
        
    },
    "field_2" : {
        "array_2": [
            {
                ... all other fields
            }
        ],
        "array_1": [
            {
               ... all other fields
            }
        ],
        // here will have 4 array or 5 array, I want to get how many array is inside this object
    },
    "field_3":{

    }

So I make my POJO class look like this:

public class MyPojo{

    @SerializedName("field_1")
    @Expose
    private Field1 class1;


    @SerializedName("field_2")
    @Expose
    private Field2 class2;

    @SerializedName("field_3")
    @Expose
    private Field3 class3;

    // other getter and setter 
}

Then I will have a Field2 class that look like this:

public class Entities {

    @SerializedName("array_1")
    @Expose
    private List<Array1Item> arraylist1 = null;
    @SerializedName("array_2")
    @Expose
    private List<Array2Item> arraylist2 = null;

    @SerializedName("array_3")
    @Expose
    private List<Array3Item> arraylist3 = null;

   //other getter setter

}

The problem now is I want to know how many array is inside field_2 object. Look like there is no way for me to access the field_2 object.

I try to access it via field2.names() , but it cant resolve.

Therefore someone please help.

What I want is, How many key like array 1, array 2 is existed inside field_2? It will return a value in int. How can I do that?

2 Answers

Try this...

try {
    int total=0;
    JSONObject json = new JSONObject(field_2);
    Iterator<String> temp = json.keys();
    while (temp.hasNext()) {
        // do your other stuffs here
        total=total+1; //at last you will get total objects in field_2

    }
} catch (JSONException e) {
    e.printStackTrace();
}

OR

JSONObject object = new JSONObject (field_2_jsonObject);
JSONArray keys = object.names ();

//KEYS SIZE WILL YOUR TOTAL LENGTH IN "field_2"
for (int i = 0; i < keys.length (); i++) {
     String key = keys.getString (i); // Here's your key

 }

Hope this will work... :)

lets say that your array object like this

public class MyObject {
@SerializedName("name")
@Expose
private String name;
@SerializedName("email")
@Expose
private String email;
// other getter and setter
}

so the parsing will be using Gson with retrofit like this

public class MyPojo {

    @SerializedName("field_1")
    @Expose
    private Field1 class1;

    @SerializedName("field_2")
    @Expose
    private Field2 class2;

    @SerializedName("field_3")
    @Expose
    private Field3 class3;
    // other getter and setter

}

public class Field2 {
private Map<String,List<MyObject>> map=new HashMap<String, List<MyObject>>();
// other getter and setter

}

so the point here is that you don't know how many keys will be like array_1,array_2, ... etc , so you use a map of string which is the key , and the value is your json Object

you can check link1, link2

Related