1. 程式人生 > >Gson 解析Json資料時候報錯: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

Gson 解析Json資料時候報錯: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

Gson報錯資訊如下:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
	at com.google.gson.Gson.fromJson(Gson.java:887)
	at com.google.gson.Gson.fromJson(Gson.java:852)
	at com.google.gson.Gson.fromJson(Gson.java:801)
	at com.telewave.terminal.activity.LoginActivity$4.onSucceed(LoginActivity.java:395)
	at com.telewave.terminal.http.HttpResponseListener.onSucceed(HttpResponseListener.java:98)
	at com.yanzhenjie.nohttp.rest.Messenger$Poster.run(Messenger.java:92)
	at android.os.Handler.handleCallback(Handler.java:761)
	at android.os.Handler.dispatchMessage(Handler.java:98)
	at android.os.Looper.loop(Looper.java:156)
	at android.app.ActivityThread.main(ActivityThread.java:6523)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
	at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
	... 13 more
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
	at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
	at com.google.gson.Gson.fromJson(Gson.java:887)
	at com.google.gson.Gson.fromJson(Gson.java:852)
	at com.google.gson.Gson.fromJson(Gson.java:801)
	at com.telewave.terminal.activity.LoginActivity$4.onSucceed(LoginActivity.java:395)
	at com.telewave.terminal.http.HttpResponseListener.onSucceed(HttpResponseListener.java:98)
	at com.yanzhenjie.nohttp.rest.Messenger$Poster.run(Messenger.java:92)
	at android.os.Handler.handleCallback(Handler.java:761)
	at android.os.Handler.dispatchMessage(Handler.java:98)
	at android.os.Looper.loop(Looper.java:156)
	at android.app.ActivityThread.main(ActivityThread.java:6523)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

伺服器後臺獲取的Json資料如下:

"[{\"$id\":\"1\",\"systemcode\":\"minalClient\",\"yyname\":\"shuyi\",\"seatno\":\"17002\",\"userid\":\"\",\"username\":\"\",\"ganid\":\"f146bbbf4ba55d680adf0c\",\"nickname\":\"陽光1\",\"status\":3},{\"$id\":\"2\",\"systemcode\":\"minalClient\",\"yyname\":\"anafnf\",\"seatno\":\"20007\",\"userid\":\"\",\"username\":\"\",\"ganid\":\"7f4d2ea93a12d39b4257c7\",\"nickname\":\"青春帥氣\",\"status\":3}]"

由於這個資料不規範,導致Json解析出現很多問題,困擾了很久,下面對上面問題說下解決方案

1、實體類如下:

public class MM {


    private String id;
    private String systemcode;
    private String yyname;
    private String seatno;
    private String userid;
    private String username;
    private String ganid;
    private String nickname;
    private int status;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSystemcode() {
        return systemcode;
    }

    public void setSystemcode(String systemcode) {
        this.systemcode = systemcode;
    }

    public String getYyname() {
        return yyname;
    }

    public void setYyname(String yyname) {
        this.yyname = yyname;
    }

    public String getSeatno() {
        return seatno;
    }

    public void setSeatno(String seatno) {
        this.seatno = seatno;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getGanid() {
        return ganid;
    }

    public void setGanid(String ganid) {
        this.ganid = ganid;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}

然後需要明白的是:在Json中

[ ... ] 代表陣列 

{ ... } 代表物件

由於該json資料的特殊性,首先得去除$ 符號,接著還需要去除 \符號

String result1 = result.replaceAll("\\\\", "");
String result2 = result1.replaceAll(Matcher.quoteReplacement("$"), "");

其中result就是該json字串資料,到這裡還有最重要的就是要將首尾引號去掉

String result3 = result2.substring(1, result2.length()-1);

這個時候就可以開心的用GSON對該資料進行解析了,不過要注意用  陣列  型別哦

MM[] mmmStr= gson.fromJson(result3, MM[].class);

Log.e("onSucceed", "mmmStr:" + Arrays.toString(mmmStr));