Gson的使用详解
2019-01-27 本文已影响9人
CarlosLynn
添加依赖:
implementation 'com.google.code.gson:gson:2.8.5'
将json解析为List集合
Type type = new TypeToken<List<TodayStepData>>() {}.getType();
List<TodayStepData> list = mGson.fromJson(stepArray, type);
将json解析为带泛型的实体类
String result = response.body().string();
Type type = new TypeToken<ResultEntity<List<SearchEntity>>>() {}.getType();
final ResultEntity<List<SearchEntity >> entity = new Gson().fromJson(result, type);