Android:用Gson解析数据

如果有iOS开发经验,并且用过MJExtension,就会发现在model里处理方式如此相像。

解析工具类
import java.util.List;

public class GsonUtil {

    /**
     *解析字符串字典
     *
     * @author 人民重重
     * @time 2019/1/31 上午9:04
     */
    public static <T> T parseJsonWithGson(String json, Class<T> type) {

        T result  = new Gson().fromJson(json, type);
        return result;
    }

    /**
     * 解析数组
     * @author 人民重重
     * @time 2019/1/31 上午10:12
     */
    public static <T> List<T> parseArrayJsonWithGson(String json, Class<T> type) {
        List<T> list = new Gson().fromJson(json, new TypeToken<List<T>>(){}.getType());
        return list;
    }
}

例1:单纯的一个字典

PersonModel
public class PersonModel {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "name = " + name + "; age = " + age + ";";
    }
}
Activity
String json = "{'name': '人民重重', 'age': 18}";
PersonModel personModel = GsonUtil.parseJsonWithGson(json, PersonModel.class);
Log.i("total", personModel.toString());
Log.i("name", personModel.getName());
Log.i("age", personModel.getAge() + "");
打印:
01-30 17:55:43.965 19300-19300/com.tencent.formalandroid I/total: name = 人民重重; age = 18;
01-30 17:55:43.966 19300-19300/com.tencent.formalandroid I/name: 人民重重
01-30 17:55:43.966 19300-19300/com.tencent.formalandroid I/age: 18

例2:字典里嵌套字典

PersonModel
public class PersonModel {
    private String name;
    private int age;
    private OtherInfoModel otherInfo;

//    这里需要声明为public,外部类调用
    public class OtherInfoModel {
        private String country;
        private String province;

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

    @Override
    public String toString() {
        return "otherInfo: {country = " + country + ", province = " + province + "}";
    }
}

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public OtherInfoModel getOtherInfo() {
        return otherInfo;
    }

    public void setOtherInfo(OtherInfoModel otherInfo) {
        this.otherInfo = otherInfo;
    }

    @Override
    public String toString() {
        return "name = " + name + ", age = " + age + ", " + otherInfo;
    }
}
Activity
String json = "{'name': '人民重重', 'age': 18, 'otherInfo': {'country': 'CN', 'province': 'Shandong'}}";
PersonModel personModel = GsonUtil.parseJsonWithGson(json, PersonModel.class);
Log.i("total", personModel.toString());
Log.i("name", personModel.getName());
Log.i("country", personModel.getOtherInfo().getCountry());
打印:
01-30 18:22:56.794 21544-21544/com.tencent.formalandroid I/total: name = 人民重重, age = 18, otherInfo: {country = CN, province = Shandong}
01-30 18:22:56.794 21544-21544/com.tencent.formalandroid I/name: 人民重重
01-30 18:22:56.794 21544-21544/com.tencent.formalandroid I/country: CN

例3:数组字典
模型和例2的相同。

Activity
String json = "[{'name': '人民重重', 'age': 18, 'otherInfo': {'country': 'CN', 'province': 'Shandong'}}," +
                        "{'name': 'SaySee', 'age': 20, 'otherInfo': {'country': 'China', 'province': 'Shandong Province'}}]";
List<PersonModel> list = GsonUtil.parseArrayJsonWithGson(json, PersonModel.class);
Log.i(TAG, "onClick: " + list);
打印:
01-31 10:22:22.494 7023-7023/com.tencent.formalandroid I/HomeActivity: onClick: 
[{name=人民重重, age=18.0, otherInfo={country=CN, province=Shandong}}, 
{name=SaySee, age=20.0, otherInfo={country=China, province=Shandong Province}}]

例4:字典里嵌套数组

PersonModel
import java.util.List;

public class PersonModel {
    private String name;
    private int age;
    private List<OtherInfoModel> otherInfo;

//    这里需要声明为public,外部类调用
    public class OtherInfoModel {
        private String country;
        private String province;

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

    @Override
    public String toString() {
        return "otherInfo: {country = " + country + ", province = " + province + "}";
    }
}

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<OtherInfoModel> getOtherInfo() {
        return otherInfo;
    }

    public void setOtherInfo(List<OtherInfoModel> otherInfo) {
        this.otherInfo = otherInfo;
    }

    @Override
    public String toString() {
        return "name = " + name + ", age = " + age + ", " + otherInfo;
    }
}
Activity
String json = "{'name': '人民重重', 'age': 18, 'otherInfo': [{'country': 'CN', 'province': 'Shandong'},{'country': 'China', 'province': 'Shandong Province'}]}";

PersonModel personModel = GsonUtil.parseJsonWithGson(json, PersonModel.class);
List<PersonModel.OtherInfoModel> list = personModel.getOtherInfo();

for (PersonModel.OtherInfoModel model : list) {
     Log.i(TAG, "onClick: " + model.getProvince());
}

猜你喜欢

转载自blog.csdn.net/weixin_34262482/article/details/87364811