zoukankan      html  css  js  c++  java
  • 生成JSON数据--Gson(谷歌)方法

    Gson生成JSON数据方法:

    创建相应的类,然后创建对象,toJson()进去就可以了
    

    要求:生成如下JSON数据

    1.{“age”:4,”name”:”zhagnsan”}

    2.{“no”:4,”name”:”zhangsan”,”employees”:[{“age”:3,”name”:”zhangsan”},{“age”:5,”name”:”lis”},{“age”:6,”name”:”wagw”}]}


    第一题代码:

    Person类

    package com.qf.demo2;
    
    public class Person {
    
        private int age;
        private String name;
        public Person(int age, String name) {
            super();
            this.age = age;
            this.name = name;
        }
        public Person() {
            super();
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return "Person [age=" + age + ", name=" + name + "]";
        }
    
    
    }
    

    获得JSON数据

    package com.qf.demo6;
    
    import com.google.gson.Gson;
    import com.qf.demo2.Person;
    
    public class Test {
    
        public static void main(String[] args) {
    
            Gson gson = new Gson();
    
            Person person = new Person(4, "zhagnsan");
    
            String  json= gson.toJson(person);
            System.out.println(json);
    
    
        }
    }
    

    第二题代码:

    Person类

    package com.qf.demo2;
    
    public class Person {
    
        private int age;
        private String name;
        public Person(int age, String name) {
            super();
            this.age = age;
            this.name = name;
        }
        public Person() {
            super();
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return "Person [age=" + age + ", name=" + name + "]";
        }
    
    
    }
    

    获得JSON数据

    package com.qf.demo6;
    
    import java.util.ArrayList;
    
    import com.google.gson.Gson;
    import com.qf.demo2.Person;
    
    public class Test3 {
    
        public static void main(String[] args) {
    
            ArrayList<Person> list = new ArrayList<>();
            list.add(new Person(3, "zhangsan"));
            list.add(new Person(5, "lis"));
            list.add(new Person(6, "wagw"));
    
            Employ employ = new Employ(4, "zhangsan", list);
    
    
            Gson gson = new Gson();
            String json = gson.toJson(employ);
            System.out.println(json);
    
    
    
    
        }
    }
    
    class Employ{
        private int no;
        private String name;
        private ArrayList<Person> employees;
        public Employ(int no, String name, ArrayList<Person> employees) {
            super();
            this.no = no;
            this.name = name;
            this.employees = employees;
        }
        public Employ() {
            super();
        }
        public int getNo() {
            return no;
        }
        public void setNo(int no) {
            this.no = no;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public ArrayList<Person> getEmployees() {
            return employees;
        }
        public void setEmployees(ArrayList<Person> employees) {
            this.employees = employees;
        }
        @Override
        public String toString() {
            return "Employ [no=" + no + ", name=" + name + ", employees=" + employees + "]";
        }
    
    }
    
  • 相关阅读:
    Atitit 趋势管理之道 attilax著
    Atitit 循环处理的新特性 for...else...
    Atitit 2017年的技术趋势与未来的大技术趋势
    atitit 用什么样的维度看问题.docx 如何了解 看待xxx
    atitit prj mnrs 项目中的几种经理角色.docx
    Atitit IT办公场所以及度假村以及网点以及租房点建设之道 attilax总结
    Atitit 工具选型的因素与方法 attilax总结
    Atitit.团队文化建设影响组织的的一些原理 法则 定理 效应 p826.v4
    Atiitt 管理方面的误区总结 attilax总结
    Atitit 未来趋势把控的书籍 attilax总结 v3
  • 原文地址:https://www.cnblogs.com/TCB-Java/p/6853999.html
Copyright © 2011-2022 走看看