zoukankan      html  css  js  c++  java
  • Gson解析数组array和list容器

    http://blog.csdn.net/lucky_bo/article/details/47657641

    Gson解析数组和list容器

    使用Gson解析首先需要加入架包文件:gson-2.2.4.jar

    定义一个类Student:

    public class Student {

    String name="xiao";

    String sex="男";

    }

    定义Java文件:

    public class ListToGson {

    public static void main(String[] args) {

    Student[] people= new Student[]{new Student(),new Student(),new Student()};

    ArrayList<Student> list = new ArrayList<Student>();

    list.add(new Student());

    list.add(new Student());

    list.add(new Student());

    String json = new Gson().toJson(list);

    String peoples = new Gson().toJson(people);

    System.out.println(json);

    System.out.println(peoples);

    }
    }

    运行结果如下:

    对list解析:[{"name":"xiao","sex":"男"},{"name":"xiao","sex":"男"},{"name":"xiao","sex":"男"}]

    对数组解析:[{"name":"xiao","sex":"男"},{"name":"xiao","sex":"男"},{"name":"xiao","sex":"男"}]

  • 相关阅读:
    junit单元测试
    方法引用
    方法引用表达式(1)
    Stream流的常用方法
    Stream流
    综合案例:文件上传
    tcp通信协议
    python 生成器与迭代器
    Python 序列化与反序列化
    python 文件操作
  • 原文地址:https://www.cnblogs.com/robbinluobo/p/7240029.html
Copyright © 2011-2022 走看看