简体   繁体   中英

Data from the JSON array in Java

I've this JSON data and after a long work I could not find a way out to get the result. My aim is to get the value of the video_url even it is null but yes I've put up a check in order to check whether something is coming or not. I've tried every possible way but all I'm getting is no output.

Here is my code:

{
"data":
    [
        {
            "type":"project-users",
            "id":"1",
            "attributes":
                   {
                    "video-url":null
                    },
            "links":
                {
                    "self": "http://localhost:3000/api/project-users/1"
                }
        },

        {   
            "type":"project-users",
            "id":"2",
            "attributes":
                {
                "video-url":null
                },
            "links":
                {
                "self":"http://localhost:3000/api/project-users/2"
                }
        },

        {
            "type":"project-users",
            "id":"3",
            "attributes":
                {
                "video-url":null
                },
            "links":
                {"self":"http://localhost:3000/api/project-users/3"
                }
        }

    ] }

In order to get the value of video_url from the attribute object I did that but failed:

private void jSONLoadData(String response) throws JSONException{

    //talk about this in getting the video url
    JSONObject obj = new JSONObject(response);
    JSONArray jArray = obj.getJSONArray("data");

    for(int i=0; i<jArray.length() ; i++){
        JSONObject jsonObj = jArray.getJSONObject(i);
        String url = jsonObj.getJSONObject("attributes").getString("video_url");
        Log.e("URL", url);

        if(url.equals(null) || url.equals("")){
            Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

Note: response is nothing but the above JSON data.

I think instead of calling getString("video_url"); first check whether video_url is null (ie isNull("video_url") ) or not.

Please try using below code.

private void jSONLoadData(String response) throws JSONException{

    //talk about this in getting the video url
    JSONObject obj = new JSONObject(response);
    JSONArray jArray = obj.getJSONArray("data");

    for(int i=0; i<jArray.length() ; i++){
        JSONObject jsonObj = jArray.getJSONObject(i);
        boolean isURLNull = jsonObj.getJSONObject("attributes").isNull("video_url");
        String url="";
        if(!isURLNull){
            url = jsonObj.getJSONObject("attributes").getString("video_url");
        }
        if(isURLNull || url.equals("")){
            Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

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