0

Below is the json response from wordpress:

[{
    "id": 711,
    "date": "2015-09-28T01:00:03",
    "date_gmt": "2015-09-27T19:30:03",
    "guid": {
        "rendered": "http:\/\/www.realcake.in\/?p=711"
    },
    "modified": "2015-09-28T01:51:52",
    "modified_gmt": "2015-09-27T20:21:52",
    "slug": "why-only-and-only-realcake",
    "type": "post",
    "link": "http:\/\/www.realcake.in\/why-only-and-only-realcake\/",
    "title": {
        "rendered": "Why only and only RealCake"
    },
    "content": {
        "rendered": "<p><img class=\"size-full wp-image-725 aligncenter\" src=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/09\/ONLY-ONLY-REAL-INGREDIENTS-FOR-REALCAKE..jpg\" alt=\"REALCAKE BAKERY IN GURGAON R.\" width=\"940\" height=\"225\" srcset=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/09\/ONLY-ONLY-REAL-INGREDIENTS-FOR-REALCAKE..jpg 940w, 
    },
    "excerpt": {
        "rendered": ""
    },
    "author": 4,
    "featured_media": 0,
    "comment_status": "open",
    "ping_status": "open",
    "sticky": false,
    "format": "standard",
    "categories": [37, 35, 1],
    "tags": [],
    "_links": {
        "self": [{
            "href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts\/711"
        }],
        "collection": [{
            "href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts"
        }],
        "about": [{
            "href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/types\/post"
        }],
        "author": [{
            "embeddable": true,
            "href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/users\/4"
        }]
    }
}, {
    "id": 420,
    "date": "2015-01-01T18:00:07",
    "date_gmt": "2015-01-01T12:30:07",
    "guid": {
        "rendered": "http:\/\/www.realcake.in\/?p=420"
    },
    "modified": "2015-08-04T23:29:55",
    "modified_gmt": "2015-08-04T17:59:55",
    "slug": "why-choose-muffins-cakes-realcake",
    "type": "post",
    "link": "http:\/\/www.realcake.in\/why-choose-muffins-cakes-realcake\/",
    "title": {
        "rendered": "Why Choose Muffins &#038; Cakes from RealCake Bakery?"
    },
    "content": {
        "rendered": "<div id=\"attachment_423\" style=\"width: 559px\" class=\"wp-caption aligncenter\"><img class=\" wp-image-423\" src=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/03\/Regular-muffins-e1425386051181-300x148.png\" 
    },
    "excerpt": {
        "rendered": "<p>. Feel free to write to us or call us if [&hellip;]<\/p>\n"
    },
    "author": 4,
    "featured_media": 0,
    "comment_status": "open",
    "ping_status": "open",
    "sticky": false,
    "format": "standard",
    "categories": [35],
    "tags": [44, 41, 46, 40, 45, 42, 38, 39, 43],
    "_links": {
        "self": [{
            "href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts\/420"
        }],
        "collection": [{
            "href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts"
        }],
        "about": [{
            "href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/types\/post"
        }],
        "author": [{
            "embeddable": true,
            "href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/users\/4"
        }]
    }
}]

==>> I am using the code below to get JSONArray:

Response response = client.newCall(request).execute();

 //here I am getting the json response(mentioned above) from wordpress rest api   

  String json = response.body().string(); 

  JSONObject object = new JSONObject(json);

//Below code is giving me null array as I dont have "Array name" in my json response.

  JSONArray array = object.optJSONArray("Name of Array in json response");

Please help me get JSONArray out of json response.

3
  • How can I attach my complete json response (from wordpress rest API) here to clarify my question in a better way. Commented May 13, 2016 at 19:02
  • can you attach your formatted json response Use jsonlint.com . Commented May 13, 2016 at 19:04
  • I have my json response in a proper formatted way but I am not able to paste here. You can run the below URL in REST Console (in chrome) to get better picture : realcake.in/wp-json/wp/v2/posts Please use GET request. Commented May 13, 2016 at 19:13

1 Answer 1

1

Looking at your JSON string, it seems that the entire response body is a JSON-Array, so you can simply parse it directly into a instance of JSONArray. This is how you can do it:

//get your JSON body-string like you do
String json = response.body().string();
//parse it directly into JSONArray
JSONArray array = new JSONArray(json);
//do whatever you want with the array...

I hope this helps you.

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

6 Comments

You beat me to it. I saw the question, thought that, and my warcraft game started and when I came back, it was answered :D
@ishmaelMakitla : I am getting jsonException in below line: JSONArray array = new JSONArray(json); org.json.JSONException: Value {"id":711,"date":"2015-09-28T01:00:03","date_gmt":"2015-09-27T19:30:03","guid":{"rendered":"http:\/\/www.realcake.in\/?p=711"},"modified":"2015-09-28T01:51:52","modified_gmt":"2015-09-\/","title":{"rendered":"Why only a\" 05-14 01:48:38.240 2236-2251/com.alok.wprest W/System.err: at org.json.JSON.typeMismatch(JSON.java:111) 05-14 01:48:38.240 2236-2251/com.alok.wprest W/System.err:
Please update your question with the updated code so that we can see your latest code changes and examine what might be causing the error and help you fix it.
Below is my method which returns json array:
Below is my method which returns json array: protected JSONArray doInBackground(String... params) { String url = params[0]; OkHttpClient client = new OkHttpClient(); Request.Builder builder = new Request.Builder(); Request request = builder.url(url).build(); try { Response response = client.newCall(request).execute(); String json = response.body().string(); try { JSONArray jArray = new JSONArray(json); return jArray; } catch(JSONException e){e.printStackTrace();} } catch (IOException e) { e.printStackTrace(); } return null; }
|

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.