Search code examples
androidjsonparsingjsonexception

validate json but JSONException unterminated object


My question has been asked several times but I couldn't find a solution for my problem.

I am trying to parse the API: 1, which is valid JSON

For that I built a function to parse the data :

private String[] getArticlesDataFromJson(String allPostsJsonStr, int nbArticlesDisplayed)
            throws JSONException {

        // These are the names of the JSON objects that need to be extracted.
        final String OWM_POSTS = "posts";
        final String OWM_ID = "id";
        final String OWM_TITLE = "title";
        final String OWM_THUMBNAIL = "thumbnail";

        JSONObject allContentsPosts = new JSONObject(allPostsJsonStr);
        JSONArray contentArticlesArray = allContentsPosts.getJSONArray(OWM_POSTS);


        String[] resultStrs = new String[nbArticlesDisplayed];
        for (int i = 0; i < contentArticlesArray.length(); i++) {
            // For now, using the format "id, title, thumbnail"
            int id;
            String title;
            String thumbnail;

            // Get the JSON object representing the article
            JSONObject article = contentArticlesArray.getJSONObject(i);


            id = article.getInt(OWM_ID);
            title = article.getString(OWM_TITLE);
            thumbnail = article.getString(OWM_THUMBNAIL);

            resultStrs[i] = id + " - " + title + " - " + thumbnail;
        }

        for (String s : resultStrs) {
            Log.v(LOG_TAG, "article :  " + s);
        }
        return resultStrs;

    }

In a AsyncTask I recover the Json String allPostsJsonStr with a buffer and I unescape the result to avoid the Unicode characters.

allPostsJsonStr = StringEscapeUtils.unescapeJava(buffer.toString());

And I call the function to parse the result :

getArticlesDataFromJson(allPostsJsonStr, 1);

In a try/catch, etc ...

And I've got an error :

Error JSONException Unterminated object at character 510 of {"status":"ok","count":1,"count_total":79,"pages":79,"posts":[{"id":1320,"type":"post","slug":"vibram-fivefingers-spyridon-mr-ou-comment-allier-legerete-et-plaisir-du-trail","url":"http://leminimaliste.info/vibram-fivefingers-spyridon-mr-ou-comment-allier-legerete-et-plaisir-du-trail/","status":"publish","title":"Vibram FiveFingers Spyridon MR ou comment allier légèreté et plaisir du Trail","title_plain":"Vibram FiveFingers Spyridon MR ou comment allier légèreté et plaisir du Trail","content":"Je souhaite aujourd’hui vous faire un retour sur mon dernier Trail en date avec mes petites nouvelles, les Spyridon MR de VFF.

I noted that it's linked with html code that appears in "content" of the JSONString, but I don't how can I rectify this error.

I need help, thank you


Solution

  • Don't unescape your json. If you need to unescape some fields you can do it after the parsing. Your call to StringEscapeUtils.unescapeJava(buffer.toString()); removes \" to replace them with " and making your string not a valid json.