type org.json.JSONArray cannot be converted to JSONObj

Viewed 98

I have held off asking for help on this one, until I have spent over a week reading every post and google and stack overflow question i can find on this error, and still nothing, so here I am. I am retrieving a list of posts from my website using Android Studio, via the WordPress API. The url is returning the correct data, but here might be an issue with the data, (it's from a Weaver Xtreme Pro Theme), but I cannot find it, so in case it is, I have included it.

the url response is, "

...Response from url: [{"id":113,"title":{"rendered":"New England\u2019s Small, Diverse Farms are a Boon for Shrubland Birds"}},{"id":110,"title":{"rendered":"Cattle raisers oppose changes to Federal water regulation"}},{"id":108,"title":{"rendered":"FCA board approves bookletter on governance of wholesale funding and related practices"}},{"id":106,"title":{"rendered":"Texas cattle raisers fight to protect water rights in Texas Supreme Court briefing"}},{"id":103,"title":{"rendered":"Episode 11 \u2013 Parlez-vous fran\u00e7ais?"}},{"id":101,"title":{"rendered":"Episode 10 \u2013 Did Spanish Colonization have a chance?"}},{"id":96,"title":{"rendered":"Episode 9 – The French make a try in Texas"}},{"id":93,"title":{"rendered":"Episode 8- The Spanish Presidios and tensions begin rising between cultures"}},{"id":90,"title":{"rendered":"Episode 7- The Spanish Missions"}},{"id":87,"title":{"rendered":"Episode 6 \u2013 A clash of cultures between the Spanish and the local peoples"}}]

The code I am using to parse it is here.

enter code here  @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();
        // Making a request to url and getting response
       String url = "https://(my website)/wp-json/wp/v2/posts?_fields[]=title&_fields[]=id";

        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);

        if (jsonStr != null){
            try {
                JSONObject parent = new JSONObject(jsonStr);
           // Getting JSON Array node
                 JSONArray Stories = parent.getJSONArray("storyList");
                // looping through All stories (i.e. the posts)
                for (int i = 0; i < Stories.length(); i++) {
                    JSONObject s = Stories.getJSONObject(i);
                    story_title = s.getString("title");
                    story_id = s.getString("id");
                         // tmp hash map for single contact
                    HashMap<String, String> story = new HashMap<>();
                    // adding each child node to HashMap key => value
                    story.put("storytitle", story_title);
                    story.put("storyid", story_id);

                    // adding the story info to the story list
                    storyList.add(story);


                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                });

            }

        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG).show();
                }
            });
        }

        return null;
    }

Logcat is showing this error on the exception:

...Json parsing error: Value [{"id":113,"title":{"rendered":"New England’s Small, Diverse Farms are a Boon for Shrubland Birds"}},{"id":110,"title":{"rendered":"Cattle raisers oppose changes to Federal water regulation"}},{"id":108,"title":{"rendered":"FCA board approves bookletter on governance of wholesale funding and related practices"}},{"id":106,"title":{"rendered":"Texas cattle raisers fight to protect water rights in Texas Supreme Court briefing"}},{"id":103,"title":{"rendered":"Episode 11 – Parlez-vous français?"}},{"id":101,"title":{"rendered":"Episode 10 – Did Spanish Colonization have a chance?"}},{"id":96,"title":{"rendered":"Episode 9 – The French make a try in Texas"}},{"id":93,"title":{"rendered":"Episode 8- The Spanish Presidios and tensions begin rising between cultures"}},{"id":90,"title":{"rendered":"Episode 7- The Spanish Missions"}},{"id":87,"title":{"rendered":"Episode 6 – A clash of cultures between the Spanish and the local peoples"}}] of type org.json.JSONArray cannot be converted to JSONObject

I realize this is similar to other posts, but for the life of me after reading them and trying to implement some of the answers, I still getting the exception error. I may have overlooked an exact answer that deals with this type of situation, so any help would be appreciated TIA

1 Answers

The mistake is that your json is an array but you try to parse it as an object.

Change this code :

JSONObject parent = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray Stories = parent.getJSONArray("storyList");
.

by

JSONArray Stories = new JSONArray(jsonStr);
Related