2

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?

2
  • 1
    The response is not the correct json format Commented Nov 26, 2016 at 10:50
  • @Nithinlal i was about to point the same Commented Nov 26, 2016 at 10:50

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

3 Comments

use optString instead of getString its will avoid null point exception
@Nithinlal yeah it's is the advice i give to everyone , i am just looking for an old post to point this out :) thanks
@DarshanGowda Glad i could help
0

You can validate your JSON here.

If entire JSON getJsonObject() is not working, then you should parse JSON objects & array in multiple arrays then use them.

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.