0

I am trying to get the URL from the following JSON response:

{
    "json": {
        "errors": [],
        "data": {
            "url": "urlIWant.com",
            "id": "asdklfjads",
            "name": "sdfjklsdf"
        }
    }
}

I have been banging my head against the wall trying to get the URL out of this response. What code do I need to access that url in Android?

Here is the latest few lines of code that I have tried to get the url.

 JSONObject json = new JSONObject(responseStr);
 json = json.getJSONObject("data"); //Get the "data" object out of the JSON
 url = json.getString("url"); //Grab the URL from the object
 Log.d("JSON URL", url); //url is null here
2
  • 1
    Can you show what you have tried? Commented Mar 24, 2016 at 3:19
  • I added the latest version of the code I have tried. Commented Mar 24, 2016 at 3:25

3 Answers 3

1

It looks like you should be grabbing from the "json" field first before you try to access "data".

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

5 Comments

Wouldn't that be what I am doing when creating the JSON Object with the response as a parameter?
No, you're just creating a json object that has a "json" data member. You would still need to access the "json" member before you could get to "data"
You're right. I grabbed the "json" jsonObject first and then was able to access the "data" object and then the URL.
It also looks like he is over-writing the content of the json Object with the dat attributes
Yeah, but that's fine in this case. The issue was with not accessing the "json" field
0

You could use retrofit along with GSON to do the work for you. There are some tutorials out there teaching how to use them.

Comments

0

1 liner:

new JSONObject(json).getJSONObject("json").getJSONObject("data").getString("url")

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.