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));


你可能感兴趣的:(Android)