0

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)
welsipieters
  • 2,412
  • 3
  • 29
  • 59

2 Answers2

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"

Community
  • 1
  • 1
Dave S
  • 3,210
  • 1
  • 16
  • 30
-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

coldblade2000
  • 25
  • 1
  • 8
  • `Log.d("UserJson", res);` equals: `D/UserRestlt: "{"email":"leerling@rocgilde.nl","screenname":"Leerling","username":"leerling"}"` – welsipieters Nov 15 '16 at 21:55
  • 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: – coldblade2000 Nov 15 '16 at 22:01
  • D/hello: {"email":"leerling@rocgilde.nl","screenname":"Leerling","username":"leerling"} – coldblade2000 Nov 15 '16 at 22:01