1

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

4
  • Are you using GSON library to parse android? Commented Aug 4, 2016 at 8:25
  • no, i'm not using it Commented Aug 4, 2016 at 8:26
  • js.equalsIgnoreCase("") try this maybe help you Commented Aug 4, 2016 at 8:47
  • json doesn't support equalsIgnoreCase(""), its for the keys. and it didn't work. but thanx Commented Aug 4, 2016 at 8:59

2 Answers 2

2

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
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I have edited your if condition. Have you checked it?
of course i did, but it still gets me the users, but without "user_info" only
can you post your json please?
so you want to get user only if it has a "profile" JSONObject right?
i tried it, it still shows the users without profiles + not showing profiles of other users
|
2

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!

1 Comment

thanx, i already tried that, it still shows me the users, but with empty value just their names

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.