Wesley Peeters Wesley Peeters - 20 days ago 6
Java Question

com.google.gson.JsonSyntaxException when parsing simple JSON string

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

{"email":"leerling@rocgilde.nl","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)

Answer

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"

Comments