0

I am trying to parse the following JSON string into a class using Gson:

{"email":"[email protected]","screenname":"Leerling","username":"leerling"}

From what I've read in the docs, its a necessity to design your classes exactly like your JSON, so I did:

public class UserDetails {
    private String email;
    private String screenname;
    private String username;
}

How is it, that something this basic, causes a com.google.gson.JsonSyntaxException?

Thanks.

** EDIT ** As requested, my usage of the Gson library:

details = gson.fromJson(res, UserDetails.class);

The res variable points to the JSON given at the top of the question.

Also, my full stack trace:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.wesleypeeters.fysio, PID: 5625
                  com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2
                      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
                      at com.google.gson.Gson.fromJson(Gson.java:803)
                      at com.google.gson.Gson.fromJson(Gson.java:768)
                      at com.google.gson.Gson.fromJson(Gson.java:717)
                      at com.google.gson.Gson.fromJson(Gson.java:689)
                      at com.wesleypeeters.fysio.users.User.<init>(User.java:51)
                      at com.wesleypeeters.fysio.activities.LoginActivity$ProcessLogin.onPostExecute(LoginActivity.java:155)
                      at android.os.AsyncTask.finish(AsyncTask.java:660)
                      at android.os.AsyncTask.-wrap1(AsyncTask.java)
                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2
                      at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
                      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
                      at com.google.gson.Gson.fromJson(Gson.java:803) 
                      at com.google.gson.Gson.fromJson(Gson.java:768) 
                      at com.google.gson.Gson.fromJson(Gson.java:717) 
                      at com.google.gson.Gson.fromJson(Gson.java:689) 
                      at com.wesleypeeters.fysio.users.User.<init>(User.java:51) 
                      at com.wesleypeeters.fysio.activities.LoginActivity$ProcessLogin.onPostExecute(LoginActivity.java:155) 
                      at android.os.AsyncTask.finish(AsyncTask.java:660) 
                      at android.os.AsyncTask.-wrap1(AsyncTask.java) 
                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method)
3
  • Please show your usage of gson and full error print Commented Nov 15, 2016 at 21:43
  • How is res defined? Commented Nov 15, 2016 at 21:46
  • @Compass as stated in the edit, res points to the JSON string earlier given. So it's a String with the value {"email":"[email protected]","screenname":"Leerling","username":"leerling"} Commented Nov 15, 2016 at 21:52

2 Answers 2

4

It seems likely that you are stripping the braces out of the res variable. Your error message typically indicates it's expecting a { but it's getting a ".

"Expected BEGIN_OBJECT but was STRING at line 1 column 1"

Sign up to request clarification or add additional context in comments.

3 Comments

Tried logging it to my console, ` Log.d("UserJson", res);` equals: D/UserRestlt: "{"email":"[email protected]","screenname":"Leerling","username":"leerling"}"
it looks like there are beginning and trailing quotes in your string object, you might try trimming them off.
Thanks! I missed those. Accepted.
-1
Expected BEGIN_OBJECT but was STRING at line 1 column 2

This tells us that there is a problem with the string you have passed it. It expects a { at the beginning, and it seems like you did provide it. Is it possible that the string you are passing to GSON isn't formatted to ignore the quotes? Try using Log.d() to print out the res variable and check it for errors

3 Comments

Log.d("UserJson", res); equals: D/UserRestlt: "{"email":"[email protected]","screenname":"Leerling","username":"leerling"}"
Dave S is correct. Those quotes before and after the string aren't supposed to be there. I used Log.d on my own app, using your string and I had a different output:
D/hello: {"email":"[email protected]","screenname":"Leerling","username":"leerling"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.