说明 | |
---|---|
首次发布 | 2019年01月30日 |
最近更新 | 2019年02月18日 |
@SerializedName()
有时候,后台返回数据的命名不是特别友好,为了方便我们自己的开发,就需要将其给的字段做个映射。那么,这时候我们就用到了@SerializedName()。
需要导入的包:import com.google.gson.annotations.SerializedName;
// Model
import com.google.gson.annotations.SerializedName;
public class TestModel {
// @SerializedName("data")
// 也可以在alternate里,设置几个后台可能返回的值
@SerializedName(value = "myData", alternate = {"MyData", "mydata", "data"})
public String myData; // 后台给的是`data`字段,而我们想设为`myData`
}
// Activity
String string = "{\"data\":\"这是data\"}";
TestModel model = new Gson().fromJson(string, TestModel.class);
Log.i(TAG, "onCreate: " + model.myData);
解析工具类
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());
}