• 生成JSON数据--官方方法


    官方生成方法:

    1)需要什么就给什么,要属性就给属性,要对象就给对象,要集合就给集合
    
    2)添加都是使用put()方法
    

    要求:

    1.生成如下JSON数据:
    {“age”:4,”name”:”zhagnsan”}

    2.生成如下JSON数据:
    [{“age”:4,”name”:”zhangsan”},{“age”:5,”name”:”lis”},{“age”:6,”name”:”wangw”}]

    3.生成如下JSON数据:
    {“no”:1,”name”:”android”,”employees”:[{“age”:3,”name”:”zhangsan”},{“age”:5,”name”:”lisi”},{“age”:2,”name”:”wangw”}]}


    第一题代码:

    package com.qf.demo5;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    public class Test {
    
        public static void main(String[] args) {
            //生成如下JSON数据:{"age":4,"name":"zhagnsan"}
            JSONObject object = new JSONObject();// 产生json数据   不需要参数
            try {
                object.put("name", "zhagnsan");
                object.put("age", 4);
                System.out.println(object);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    }
    

    第二题代码:

    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 + "]";
        }
    
    
    }
    
    package com.qf.demo5;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import com.qf.demo2.Person;
    
    public class Test2 {
        public static void main(String[] args) {
            //生成如下JSON数据:[{"age":4,"name":"zhangsan"},{"age":5,"name":"lis"},{"age":6,"name":"wangw"}]
            List<Person> list = new ArrayList<>();
            list.add(new Person(4, "zhangsan"));
            list.add(new Person(5, "lis"));
            list.add(new Person(6, "wangw"));
    
            JSONArray array = new JSONArray();
            for (int i = 0; i < list.size(); i++) {
                JSONObject object = new JSONObject();
                try {
                    object.put("name", list.get(i).getName());
                    object.put("age", list.get(i).getAge());
                    array.put(object);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println(array);
        }
    }
    

    第三题代码:

    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 + "]";
        }
    
    
    }
    
    package com.qf.demo5;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import com.qf.demo2.Person;
    
    public class Test3 {
    
        public static void main(String[] args) {
            //生成如下JSON数据:{"no":1,"name":"android","employees":[{"age":3,"name":"zhangsan"},{"age":5,"name":"lisi"},{"age":2,"name":"wangw"}]}
            JSONObject object = new JSONObject();
            try {
                object.put("no", 1);
                object.put("name", "android");
    
                List<Person> persons = new ArrayList<>();
                persons.add(new Person(3, "zhangsan"));
                persons.add(new Person(5, "lisi"));
                persons.add(new Person(2, "wangw"));
    
                object.put("employees", persons);
                System.out.println(object);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
  • 相关阅读:
    Cobalt Strike系列教程第二章:Beacon详解
    Cobalt Strike系列教程第一章:简介与安装
    安全意识第九期丨软件暗藏陷阱 下载要三思
    安全意识第八期丨OMG!发个帖子竟然摊上大事了
    新手教程丨利用Python制作一款截图识别软件!
    2019互联网安全城市巡回赛·北京站——巅峰对决,邀你来战!
    安全意识第七期丨双11优惠券“1元抵1000”,速来领!
    《0day安全软件漏洞分析技术》学习笔记
    安全意识第六期丨谁来帮帮我,改签机票为什么被扣2万元?
    C#常用功能函数小结(.NET 4.5)
  • 原文地址:https://www.cnblogs.com/TCB-Java/p/6854000.html
Copyright © 2020-2023  润新知