Gson将 json转换成 javaBean

在使用GSON解析一段JSON数组时,需要借助TypeToken将期望解析成的数据类型传入到fromJson()方法中,如下:

List<Person> people = gson.fromJson(jsonData, new TypeToken<List<Person>>(){}.getType());

首先,new TypeToken<List<Person>>(){}是一个匿名内部类,其等价MyTypeToken<List<Person>> extends TypeToken(){},但是{}里是空的,

 

1 :  Gson 将 jsonList转换成 List  通过下面的 函数转换

      gson.fromJson(result.toString(), new TypeToken<List<LevelTypeInfo>>(){}.getType())

String JsonList = 

[
  {
    "codeType": "S",
    "description": "武汉"
  },
  {
    "codeType": "C",
    "description": "北京"
  },
  {
    "codeType": "B",
    "description": "长沙"
  },
  {
    "codeType": "W",
    "description": "深圳"
  }
]
public List<LevelTypeInfo> getLevelTypeList(String assetName) {
        try {
            InputStream open = BaseApplication.getContext().getAssets().open(assetName);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(open));
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                result.append(line);
            }
            bufferedReader.close();
            Gson gson = new Gson();
            return gson.fromJson(result.toString(), new TypeToken<List<LevelTypeInfo>>(){}.getType());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
public class LevelTypeInfo implements Serializable {
   
    private String codeType;
    
    private String description;

    public String getCodeType() {
        return codeType;
    }

    public void setCodeType(String codeType) {
        this.codeType = codeType;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "LevelTypeInfo{" +
                "codeType='" + codeType + '\'' +
                ", StringType='" + description + '\'' +
                '}';
    }
}

2 : 通过Gson 将 json 转换成 对象  直接转换成 Student对象

 return gson.fromJson(result.toString(), Student.class);

{
  "studentList": [
    {
      "code": "01",
      "name": "张三",
      "description": "武汉"
    },
    {
      "code": "02",
      "name": "李四",
      "description": "北京"
    }
  ]
}
public Student getLevelList() {

        try {
            InputStream open = BaseApplication.getContext().getAssets().open("level.json");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(open));
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                result.append(line);
            }
            bufferedReader.close();
            Gson gson = new Gson();
            return gson.fromJson(result.toString(), Student.class);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

public class Student {
    private List<StudentInfo> studentList;

    public class StudentInfo {
        private String code;
        private String name;
        private String description;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

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

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值