gson的使用

1:toJson的用法(当参数是bean对象时,将javabean转换为json数据)

Gson gson=new Gson();
                xuesheng xuesheng[]={new xuesheng("laoj",10,99),new xuesheng("laoja",33,11),new xuesheng("laowang",99,12)};
                String s=gson.toJson(xuesheng);//将javabean转换为json数据
                Log.i("json",s.toString());
                /*输出信息为:
                11-16 21:03:07.485 11317-11317/com.example.liang.myapplication I/json: [{"name":"laoj","score":99.0,"xuehao":10},{"name":"laoja","score":11.0,"xuehao":33},{"name":"laowang","score":12.0,"xuehao":99}]*/

bean文件为:

public class xuesheng
{
    private String name;
    private int xuehao;
    private float score;

    public xuesheng() {
    }

    public xuesheng(String name, int xuehao, float score) {
        this.name = name;
        this.xuehao = xuehao;
        this.score = score;
    }

    public String getName() {
        return name;
    }

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

    public int getXuehao() {
        return xuehao;
    }

    public void setXuehao(int xuehao) {
        this.xuehao = xuehao;
    }

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "xuesheng{" +
                "name='" + name + "'" +
                ", xuehao=" + xuehao +
                ", score=" + score +
                '}';
    }
}

2:toJson的用法(当参数是List时)

 Gson gson=new Gson();
                List<String> list= Arrays.asList("laoli","28","娜娜","liua");
                String s=gson.toJson(list);//将List转换为json数据
                Log.i("json",s.toString());
                /*输出信息为:
                11-16 21:13:17.575 22693-22693/com.example.liang.myapplication I/json: ["laoli","28","娜娜","liua"]*/

3:toJson的用法(当参数是Map时)

Gson gson=new Gson();
                Map<String,Object> map=new HashMap<>();
                map.put("name","laowang");
                map.put("age",20);
                map.put("school","mazhong");
                Log.i("xinxi",gson.toJson(map));
                /*输出信息为:
                11-16 21:17:25.615 27567-27567/com.example.liang.myapplication I/xinxi: {"name":"laowang","age":20,"school":"mazhong"}*/

4:fromJson的用法(将json数据转换为bean,转换单条json数据)

Gson gson=new Gson();
                String studentJsonStr="{\"name\":\"xuanyouwu\",\"xuehao\":100,\"score\":26}";
                xuesheng xuesheng=gson.fromJson(studentJsonStr,xuesheng.class);
                Log.i("xingxi",xuesheng.toString());
                /*输出信息为:
                11-16 21:23:36.095 3735-3735/com.example.liang.myapplication I/xingxi: xuesheng{name='xuanyouwu', xuehao=100, score=26.0}*/

5:fromJson的用法(将json数据转换为bean,转换多条json数据)

 Gson gson=new Gson();
                String studentJsonStr="[{\"name\":\"xuanyouwu\",\"xuehao\":100,\"score\":26},{\"name\":\"laofu\",\"xuehao\":200,\"score\":216},{\"name\":\"laoliang\",\"xuehao\":10,\"score\":6}]";
                List<xuesheng> xuesheng=gson.fromJson(studentJsonStr,new TypeToken<List<xuesheng>>(){}.getType());
                Log.i("xingxi",xuesheng.toString());
                /*输出信息为:
                11-16 21:28:53.855 10282-10282/com.example.liang.myapplication I/xingxi: [xuesheng{name='xuanyouwu', xuehao=100, score=26.0}, xuesheng{name='laofu', xuehao=200, score=216.0}, xuesheng{name='laoliang', xuehao=10, score=6.0}]*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 关于json,gson是最常用到的一个库。平常使用时我通常使用Gson gson = new Gson();的方式...
    黑泥卡阅读 10,508评论 1 14
  • Gson是一个可用于将Java对象转换为JSON表示形式的Java库。也可用于将JSON字符串转换为等效的Java...
    毕丙伟阅读 2,047评论 0 1
  • 测试的pojo 需求 1.当pojo类中有int类型,但是前端获得的json数据为age为空的情况下 2.序列化时...
    SingleException阅读 323评论 0 0
  • 1.注解插件ButterKnife Zelezny的使用与安装 1.1.下载安装 Settings——>Plugi...
    魏成阅读 513评论 0 0
  • 雄赳赳,气昂昂……
    快乐每一天ABC阅读 2,455评论 0 5

友情链接更多精彩内容