JSON数据原文:

[{id:1,name:"酒吞童子",cv:"阪口周平"},{id:2,name:"茨木童子",cv:"福山润"},{id:3,name:"大天狗",cv:"前野智昭"},{id:4,name:"荒川之主",cv:"子安武人"}]

首先进行gson依赖的引入:

Android Studio内build.gradle(app)添加

compile 'com.google.code.gson:gson:2.2.4'

新建java bean类

其中变量名称需与json数组键名相同

public class ShikigamiList{
        @SerializedName("id")//为防止混淆后无法匹配
        public int id;
        @SerializedName("name")
        public String name;
        @SerializedName("cv")
        public String cv;

        public int getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCv() {
            return cv;
        }

        public void setCv(String cv) {
            this.cv = cv;
        }
    }

使用Gson进行解析:

        //  解析JSON数据
        Gson gson = new Gson();
        List<Shikigami.ShikigamiList> shikigamiLists = gson.fromJson(json, new TypeToken<List<Shikigami.ShikigamiList>>() {}.getType());
        // 取得数据
     String name = shikigamiLists.get(index).name;
        String cv = shikigami.get(index).cv;   

Release混淆设置:

proguard-rules.pro内增加如下,

-dontwarn
#okhttp混淆配置
-keep class com.squareup.okhttp.** { *;}
-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
#Gson混淆配置
-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson 下面替换成自己的实体类

OK!

发表评论

电子邮件地址不会被公开。 必填项已用*标注