简体   繁体   中英

Parsing JSON data in Android/Java

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

似乎您应该先尝试从“ json”字段中获取内容,然后再尝试访问“数据”。

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

1个班轮:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM