Darshan Gowda Darshan Gowda - 14 days ago 10
Java Question

How to access JSON data with multiple array objects : android

I am stuck in getting the data from the JSON file with multiple data sets.

{"status":"ok","count":3,"count_total":661,"pages":133,"posts":
[{"id":20038,"type":"post","slug":"xperia-launcher-download","url":"http:\/\/missingtricks.net\/xperia-launcher-download\/","status":"publish","title":"Download Xperia Launcher app for Android (Latest Version)",
{"id":94,"type":"post","slug":"top-free-calling-apps-of-2014-year","url":"http:\/\/missingtricks.net\/top-free-calling-apps-of-2014-year\/","status":"publish","title":"Best Free Calling Apps for Android November 2014",
{"id":98,"type":"post","slug":"top-free-calling-apps-of-2016-year" "url":"http:\/\/missingtricks.net\/top-free-calling-apps-of-2016-year\/","status":"publish","title":"Best Free Calling Apps for Android December 2016"}]}


I need to access the title, url and status from the above JSON file.

@Override
protected void onPostExecute(String result) {

//this method will be running on UI thread

pdLoading.dismiss();
List<DataFish> data = new ArrayList<>();

pdLoading.dismiss();
try {

JSONArray jArray = new JSONArray(result);

// Extract data from json and store into ArrayList as class objects
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);


DataFish fishData = new DataFish();
fishData.status = json_data.getString("status");

fishData.title = json_data.getString("url");

fishData.sizeName = json_data.getString("title");


data.add(fishData);
}



} catch (JSONException e) {
Toast.makeText(JSonActivity.this, e.toString(), Toast.LENGTH_LONG).show();
Log.d("Json","Exception = "+e.toString());
}

}


I am getting a JSONException with the above code.

What should I do to access the title,status and url from the JSON File?

Answer

You have to fetch your JSONArray which is inside a JSONObject , so create a JSONObject and fetch your array with index "posts"

1.) result is a JSONObject so create a JSONObject

2.) Fetch your JSONArray with index value as "posts"

3.) Now simply traverse array objects by fetching it through index

        JSONObject jObj = new JSONObject(result);
        JSONArray jArray = jObj.getJSONArray("posts");

        // Extract data from json and store into ArrayList as class objects
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);


            DataFish fishData = new DataFish();
            fishData.status = json_data.getString("status");

            fishData.title = json_data.getString("url");

            fishData.sizeName = json_data.getString("title");


            data.add(fishData);
        }

Note : i don't know weather it is a sample response with shorter version though your json object should ends with } not with , .

[{"id":20038,"type":"post","slug":"xperia-launcher-download","url":"http://missingtricks.net/xperia-launcher-download/","status":"publish","title":"Download Xperia Launcher app for Android (Latest Version)",

//  ^^^ there should be a } not a , to end json
// so make sure to do the correction so it will look like =>  ...st Version)"}, 

{"id":94,"type":"post","slug":"top-free-calling-apps-of-2014-year","url":"http://missingtricks.net/top-free-calling-apps-of-2014-year/","status":"publish","title":"Best Free Calling Apps for Android November 2014", ]

Improvements :

you can use optString to avoid null or non-string value if there is no mapping key

This has two variations

Get an optional string associated with a key. It returns the defaultValue if there is no such key.

 public String optString(String key, String defaultValue) {
  fishData.status = json_data.optString("status","N/A");
 // will return "N/A" if no key found 

or To get empty string if no key found then simply use

  fishData.status = json_data.optString("status");
 // will return "" if no key found where "" is an empty string