解析简单的JSON字符串时出现com.google.gson.JsonSyntaxException

时间:2016-11-15 21:37:48

标签: java android json gson

我正在尝试使用Gson将以下JSON字符串解析为类:

{"email":"leerling@rocgilde.nl","screenname":"Leerling","username":"leerling"}

从我在文档中读到的内容来看,设计类的必要性与JSON完全相同,所以我做了:

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

如何,这个基本的东西导致com.google.gson.JsonSyntaxException

感谢。

**编辑** 根据要求,我对Gson库的使用:

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

res变量指向问题顶部给出的JSON。

另外,我的完整堆栈跟踪:

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)

2 个答案:

答案 0 :(得分:4)

你似乎可能正在从res变量中删除大括号。您的错误消息通常表示它预期{,但它会获得"

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

答案 1 :(得分:-1)

Expected BEGIN_OBJECT but was STRING at line 1 column 2

这告诉我们您传递的字符串存在问题。它在开始时期望{,看起来你确实提供了它。您传递给GSON的字符串是否可能被格式化为忽略引号?尝试使用Log.d()打印出res变量并检查错误