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

    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":"男"}]





  • 相关阅读:
    每天一个命令
    2017-2-21
    egrep []+ 和awk总结
    ifconfig eth0 取行取列
    stat /etc/hosts 取行取列644
    压缩解压缩
    目录
    公告
    To do list
    CSP 2019 游记
  • 原文地址:https://www.cnblogs.com/llguanli/p/6855269.html
Copyright © 2011-2022 走看看