1

I have this json

{
    "results": [
        {
            "user": {
                "gender": "male",
                "name": {
                    "title": "mr",
                    "first": "herbert",
                    "last": "davidson"
                },
                "location": {
                    "street": "9763 fincher rd",
                    "city": "melbourne",
                    "state": "new south wales",
                    "zip": 26278
                },
                "email": "[email protected]",
                "username": "lazyelephant581",
                "password": "abgrtyu",
                "salt": "O8ZbSsUL",
                "md5": "7575bd959be09bc6d510a6a91750ce40",
                "sha1": "0db99de8402e1defbd7935dbd602d99329698d4d",
                "sha256": "c283d262115f90b729bb555db3dbecc8d27eebed513d7f01da799dec9e9d3269",
                "registered": 1275070071,
                "dob": 122409280,
                "phone": "05-5742-3228",
                "cell": "0481-652-548",
                "TFN": "973720989",
                "picture": {
                    "large": "https://randomuser.me/api/portraits/men/24.jpg",
                    "medium": "https://randomuser.me/api/portraits/med/men/24.jpg",
                    "thumbnail": "https://randomuser.me/api/portraits/thumb/men/24.jpg"
                }
            }
        }
    ],
    "nationality": "AU",
    "seed": "0a69317ece7072e000",
    "version": "0.7"
}

and im doing this:(result is the json returned from the service)

try{
    JSONObject jsonResponse = new JSONObject(result);
    JSONObject jo = jsonResponse.getJSONObject("user");
    String nome = jo.getString("username"); 
}catch(Exception e) {
    e.printStackTrace();
}

but gives me this error

org.json.JSONException: No value for username

What im doing wrong? I tryed to do a JSONArray, tryed to get the jsonobject from another jsonobject, but giver an error that cannot convert jsonobject to a jsonarray.

regards!

Rafael

2 Answers 2

6

Try this in your code .

try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray results = jsonObject.getJSONArray("results");
        for (int i = 0; i < results.length(); i++) {
            JSONObject jo = results.getJSONObject(i);
            JSONObject user = jo.getJSONObject("user");
            String username = user.optString("username");
        }
} catch (JSONException e) {
        e.printStackTrace();
}

Use optString in your code .If your username is null,it will not return error .

Sign up to request clarification or add additional context in comments.

1 Comment

@RafaelSpessotto Good luck for you!
1

The first-level key in your dictionary is "results", not "user". Also, results is an array, so you have to index into that.

You're trying to access: user->username

You need to access: results[0]->user->username

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.