Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 554
user3359419 Avatar asked Mar 13 '14 03:03

user3359419


1 Answers

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()));
like image 180
Ravi Vaniya Avatar answered Sep 18 '22 00:09

Ravi Vaniya