user3359419
user3359419

Reputation: 71

org.json.JSONException: Unterminated object at character on Android

I parse the JSON String and face the error

org.json.JSONException: Unterminated object at character 25

my JSON is retrieved from Facebook

{Response:  responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[{"venue":{"id":108242939200809,"zip":"","longitude":11.4,"latitude":62.5833,"street":""},"location":"Røros, Norway","eid":1473462312875161,"pic_big":"https:\/\/fbcdn-profile-a.akamaihd.net\/static-ak\/rsrc.php\/v2\/yn\/r\/5uwzdFmIMKQ.png","pic_small":"https:\/\/fbcdn-profile-a.akamaihd.net\/static-ak\/rsrc.php\/v2\/yy\/r\/XcB-JGXohjk.png","description":"Test","name":"Test"},{"venue":{"id":105818232792451,"zip":"","longitude":108.217,"latitude":16.0167,"street":""},"location":"Hòa Vang","eid":1425682134338854,"pic_big":"https:\/\/fbcdn-profile-a.akamaihd.net\/static-ak\/rsrc.php\/v2\/yn\/r\/5uwzdFmIMKQ.png","pic_small":"https:\/\/fbcdn-profile-a.akamaihd.net\/static-ak\/rsrc.php\/v2\/yy\/r\/XcB-JGXohjk.png","description":"test","name":"Test"}]}}, error: null, isFromCache:false}

My JSON parser is

public static JSONArray parse(Response response) throws JSONException{          
        JSONArray jsonArray=new JSONArray(response.toString());
        return jsonArray;
    }

Please help me. Thank you very much.

Upvotes: 7

Views: 39924

Answers (6)

Ravi Vaniya
Ravi Vaniya

Reputation: 1611

This is, May be because you are trying to parse response after coverting your response into String with response.toString()

So if your response is

{"title":"This is Title","message":"This is message"}

and you converted it to String with response.toString()

Then your response will be like this

{title:This is Title,message:This is message}

So you are trying to parse response of type String and the compiler will not be able to parse that response and it will throws an error like Unterminated object at character......

So make sure that you are parsing valid JSON.

EDIT ( Credit : Dhruv Raval for below edited solution )

You may solve this by:

Before:

GraphResponse response;
JSONObject jObjResponse = new JSONObject(response.toString());

After:

GraphResponse response;
JSONObject jObjResponse = new JSONObject(String.valueOf(response.getJSONObject()));

Upvotes: 9

Keshav Gera
Keshav Gera

Reputation: 11264

try {
            JSONObject jsonData = response.getJSONObject();
            JSONArray jsonArray = jsonData.getJSONArray("data");

            Log.e("MainActivity ", "jsondata   -->  " + jsonArray.toString(2));
            Log.e("MainActivity ", "jsonArray.length()->  " + jsonArray.length());

            likesPojos.clear();
            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject json = jsonArray.getJSONObject(i);

                Log.e("MainActivity ", "message  -->  " + json.getString("name"));
                Log.e("MainActivity ", "id  -->  " + json.getString("id"));

                LikesPojo likesPojo = new LikesPojo();
                likesPojo.setId(json.getString("id"));
                likesPojo.setName(json.getString("name"));
                likesPojos.add(likesPojo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

Upvotes: 0

Rubi
Rubi

Reputation: 396

Just use JSONObject graphObj =response.getJSONObject(); and you will get the graphObject.

Upvotes: 3

Harit Dahiya
Harit Dahiya

Reputation: 206

Change

JSONArray jsonArray=new JSONArray(response.toString());

to

JSONObject start = new JSONObject(String.valueOf(response.getJSONObject()));

then direct call

 JSONArray data = start.getJSONArray("data");

It gives you the "Data" JSON Array.

Upvotes: 0

Dhruv Raval
Dhruv Raval

Reputation: 5034

i m getting same error & solving them like:

GraphResponse response;

Before:

       JSONObject jObjResponse = new JSONObject(response.toString());

After:

JSONObject jObjResponse = new JSONObject(String.valueOf(response.getJSONObject()));

Upvotes: 3

Gabe Sechan
Gabe Sechan

Reputation: 93678

Your JSON is invalid. All variable names need to be quoted.

Upvotes: 2

Related Questions