Alimov Shohrukh Alimov Shohrukh - 4 months ago 29
JSON Question

How to skip json object (from json array) in android if it has an empty/null value?

I'm developing an app where user can get list of other users, but i have to skip the users if they dont have completed their profile

ok take a look at my code:

Iterator<String> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next();
JSONObject js = json.optJSONObject(key);
if (js != null) {
String user_info = js.getString("user_info");
user.setInfo(user_info);
} else {

}
}


what should i do in (else)

heres my json:

"users": [
{
"id": 73,
"name": "Name",
"lastname": "Lastname",
"email": "",
"create_date": 1469874203
},
{
"id": 73,
"name": "Name",
"lastname": "Lastname",
"email": "",
"create_date": 1469874203
"profile"{
"address": string...
"avatar": url...
}

},


something like that

Answer

Do it like:

JSONObject jsonObject = new JSONObject(responseString);
JSONArray jsonArray = jsonObject.getJSONArray("users");
if(jsonArray.lenght() > 0){
    for(int i=0; i<jsonArray.length(); i++)
    {
        JSONObject object = jsonArray.getJSONObject(i);
        if(object.has("profile")){
            Iterator<String> keys = object.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                if(TextUtils.isEmpty(key)){
                    break;
                }
                else{
                    User user = new User();
                    //parse and save user details
                }
            }
        }
    }
}