org.json.JSONException: Unterminated object at character on Android
Asked Answered
E

6

7

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.

Egret answered 13/3, 2014 at 3:6 Comment(1)
Have you tried using Volley + GSON? It's pretty easy to implement. Check out the tutorial: blog.aimanbaharum.com/2014/02/…Breeze
P
9

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()));
Porcelain answered 14/8, 2018 at 14:58 Comment(5)
Please provide the solution to this problem. Thanks!Ledford
@DakshGargas debug the code before parsing JSON and make sure that the JSON is valid ( in this case 'not string' ) and for solution see my updated answer.Porcelain
Yeah, I had fixed it... I just added that comment so you can help future visitors.Ledford
Well Thx for that :)Porcelain
What would be the solution for this problem in kotlin? I tried String.valueOf but it doesn't exist somehow.Reste
A
3

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()));
Aliquot answered 23/10, 2015 at 9:18 Comment(0)
S
3

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

Shalna answered 30/11, 2015 at 7:54 Comment(0)
W
2

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

Wilson answered 13/3, 2014 at 3:24 Comment(2)
Hi Gabe, can you explain more? This is what I get from Facebook. How to correct them? Thank you.Egret
Notice how all the variable names after state have "" around them? That's right. See how the first few don't? That's invalid. It causes a crash when you try to parse it, because it doesn't know where the variable name ends without it.Wilson
F
0

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.

Fatuitous answered 31/3, 2016 at 8:15 Comment(0)
B
0
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();
        }
Breedlove answered 7/7, 2017 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.