2

I'm currently trying to parse some JSON in my android application but I keep getting an error saying "No value for users"

This is the current code I am using:

JSONObject parentObject = new JSONObject(finalJSON);

            JSONObject childObject = new JSONObject(String.valueOf(parentObject));

            JSONArray parentArray = childObject.getJSONArray("users");

            JSONObject finalObject = parentArray.getJSONObject(0);

            String userName = finalObject.getString("username");

            String profileLink = finalObject.getString("profileimage");

            Log.d("JSON", String.valueOf(finalObject));

            return profileimage+ " - " + profileLink ;

and here is my JSON:

{
"reply": {
    "users": [
        {
            "userid": "001",
            "loggedIn": 1,
            "username": "joe.bloggs",
            "profileimage": "http://127.0.0.1/joebloggs.png",
            "realname": "Joe Bloggs",
        }
    ]

   }
}

Thanks in advance!

3 Answers 3

3

You forgot the reply level.

Here is what you should do.

JSONObject parentObject = new JSONObject(finalJSON);


JSONArray userArray = parentObject
                .getJSONObject("reply")
                .getJSONArray("users");

JSONObject finalObject = userArray.getJSONObject(0);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, so I've just tried that but it is asking me to wrap it as an integer?
Nevermind, I tried to do .getJSONArray("reply").getJSONArray("users").
0

you can't take new object for object inside object.

you have to do this for parse object inside object.

JSONObject object1 =  new JSONObject(finalJSON);

JSONObject object2 =  object1.getJSONObject("reply");

JSONArray jsonArray = object2.getJSONArray("users");

JSONObject object3 =  jsonArray.getJSONObject(0);

Comments

0

try the following:

JSONObject root = null;
try {
    root = new JSONObject("reply");
    JSONArray childArray = root.getJSONArray("users");
    String userName = childArray.getString(0);
    .
    .
    .
} catch (JSONException e) {
    e.printStackTrace();
}

Comments

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.