Paul
Paul

Reputation: 4428

String org.json.JSONException: Unterminated object at character

I'm trying to make an http call to a json, the json is very long.

When trying to do this:

 JSONObject json = new JSONObject(htmlString);

Error:

 at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
        at org.json.JSONTokener.readObject(JSONTokener.java:393)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONTokener.readArray(JSONTokener.java:429)
        at org.json.JSONTokener.nextValue(JSONTokener.java:103)
W/System.err:     at org.json.JSONTokener.readObject(JSONTokener.java:384)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONTokener.readObject(JSONTokener.java:384)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONTokener.readArray(JSONTokener.java:429)
        at org.json.JSONTokener.nextValue(JSONTokener.java:103)
        at org.json.JSONTokener.readObject(JSONTokener.java:384)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONTokener.readObject(JSONTokener.java:384)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONObject.<init>(JSONObject.java:156)
        at org.json.JSONObject.<init>(JSONObject.java:173)
         .......

Code:

 private class Html extends AsyncTask<Void, Void, Void> {
            private String url = "https://pastebin.com/raw/2T93TvDU";
            private JSONArray array = new JSONArray();

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Log.v("Class:" + TAG, "doInBackground:" + url);

                    Document doc = Jsoup.connect(url).get();
                    String htmlString = doc.toString();
                    htmlString = htmlString.replaceAll("<html>", "");
                    htmlString = htmlString.replaceAll("</html>", "");
                    htmlString = htmlString.replaceAll("<head>", "");
                    htmlString = htmlString.replaceAll("</head>", "");
                    htmlString = htmlString.replaceAll("<body>", "");
                    htmlString = htmlString.replaceAll("</body>", "");


                    try {
                        Log.v("Class:" + TAG, "Js:" + htmlString);
                        JSONObject json = new JSONObject(htmlString);
                        //Object list = json.get("list");
                        Log.v("Class:" + TAG, "Json:" + json);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                populateListItem(array);
            }


        }

Upvotes: 0

Views: 2709

Answers (1)

Leo Aso
Leo Aso

Reputation: 12463

The data at that URL is not HTML, it's pure JSON (sent as text/plain). Using connect().get() will attempt to turn it into a HTML document, incorrectly encoding and decoding the data and returning an incorrect result. Instead use connect().execute().body() to get the JSON string directly.

@Override
protected Void doInBackground(Void... params) {
    try {
        String jsonString = Jsoup.connect(url).execute().body();
        JSONObject json = new JSONObject(jsonString);
        JSONArray list = json.getJSONObject("list").getJSONArray("item");
        // now you can use the list
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }
    return null;
}

Upvotes: 1

Related Questions