0

This is my JSON (full JSON is here : https://pastebin.com/kyPMWcTT):

 "replies": [
        [
           {
              "id": 2,
              "parent": 0,
              "author": 1,
              "author_name": "admin",
              "author_url": "http://localhost/wordpress",
              "date": "2021-05-02T08:38:00",
              "content": {
                 "rendered": "<p>Nice Blog, Awesome !</p>\n"
              },
              "link": "http://localhost/wordpress/2021/05/01/one-pot-thai-style-rice-noodles/#comment-2",
              "type": "comment",
             
              }
        ]

I am trying to get the rendered item which is inside the content Object . This is the Code i have tried :

   JSONArray replyArray = embeddedObject.getJSONArray("replies");
                            for (int j = 0; j < replyArray.length(); j++) {
                                JSONObject contentObject = replyArray.getJSONObject(j);
                                JSONObject getContent = contentObject.getJSONObject("content");
                                String reply = getContent.getString("rendered");
                                Log.e("Reply is", reply);
                            }

But, the Logcat output is :

Value at 0 of type org.json.JSONArray cannot be converted to JSONObject

How to solve this problem ? What am i doing wrong ? Please Guide

3
  • Your JSON has a nested array for some reason? It goes replies -> array -> array if you have a look at it properly. Commented May 3, 2021 at 11:32
  • @HenryTwist So, how do I parse this nested Array ? It has no name at all Commented May 3, 2021 at 17:20
  • 1
    You can use JSONArray.getJSONArray to obtain items in an array, which you're already using in your code. Commented May 3, 2021 at 17:50

1 Answer 1

1

What's Happening?

We are trying to fetch the JSONObject immediately after replies JSONArray. But, the actual content lies in the following hierarchy.

replies JSONObject -> JSONArray -> JSONArray -> ContentObject -> Content

Solution

Replace this

JSONArray replyArray = embeddedObject.getJSONArray("replies");
for (int j = 0; j < replyArray.length(); j++) {
      JSONObject contentObject = replyArray.getJSONObject(j);
      JSONObject getContent = contentObject.getJSONObject("content");
      String reply = getContent.getString("rendered");
      Log.e("Reply is", reply);
}

With this

JSONArray replyArray = embeddedObject.getJSONArray("replies");
for (int j = 0; j < replyArray.length(); j++) {
      JSONArray replySubArray = replyArray.getJSONArray(j);
      for (int i = 0; i < replySubArray.length(); j++) {
            JSONObject contentObject = replySubArray.getJSONObject(i);
            JSONObject getContent = contentObject.getJSONObject("content");
            String reply = getContent.getString("rendered");
            Log.e("Reply is", reply);
      }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks So much Buddy !

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.