Reputation: 65
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
Upvotes: 1
Views: 2760
Reputation: 444
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
}
}
}
}
}
Upvotes: 2
Reputation: 650
It can be done by using this code,
if (myObject.has("object_name") && !myObject.isNull("object_name")) {
// Consider this user.
}else{
// Avoid this user.
}
Also you can directly check the null by myObject.isNull("object_name")
in if block.
Hope this helps!
Upvotes: 2