验证 json 但 JSONException 未终止对象

validate json but JSONException unterminated object

我的问题已经被问过好几次了,但我找不到解决问题的方法。

我正在尝试解析 API: 1,这是有效的 JSON

为此,我构建了一个函数来解析数据:

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;

    }

在 AsyncTask 中,我用缓冲区恢复了 Json 字符串 allPostsJsonStr,然后取消转义结果以避免出现 Unicode 字符。

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

然后我调用函数来解析结果:

getArticlesDataFromJson(allPostsJsonStr, 1);

在try/catch等...

我有一个错误:

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.

我注意到它与 JSON 字符串的 "content" 中出现的 html 代码相关联,但我不知道如何纠正此错误。

我需要帮助,谢谢

不要取消转义您的 json。如果您需要取消转义某些字段,您可以在解析后进行。 您拨打 StringEscapeUtils.unescapeJava(buffer.toString());删除 \" 以将它们替换为 " 并使您的字符串无效 json.