各种类型的对象的输出格式总结

引入jar包

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.58</version>
		</dependency>

ObjectOutputFormat.java

package com.example.demo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.User;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 各种类型的对象的输出格式总结
 */
public class ObjectOutputFormat {

    /**
     * int数组输出格式
     *
     * [1, 2, 3]
     */
    public static void test3() {
        int[] nums = {1,2,3};
        System.out.println(Arrays.toString(nums));
    }

    /**
     * JSONObject以jsonString形式把存储的int数组输出格式
     *
     * {
     *     "msg":"success",
     *     "code":"200",
     *     "data":[
     *         1,
     *         2,
     *         3
     *     ]
     * }
     */
    public static void test4() {
        //1. 初始化数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "200");
        jsonObject.put("msg", "success");
        int[] nums = {1,2,3};
        jsonObject.put("data", nums);

        //2. 对象转json字符串
        String jsonString = JSON.toJSONString(jsonObject);

        //3. 输出
        System.out.println(jsonString);
    }

    /**
     * JSONArray中存储的int数组输出格式
     *
     * [1,2,3]
     */
    public static void test5() {
        //1. 初始化数据
        int[] nums = {1,2,3};
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "200");
        jsonObject.put("msg", "success");
        jsonObject.put("data", nums);

        //2. 对象转json字符串
        String jsonString = JSON.toJSONString(jsonObject);

        //3. 解析jsonString
        JSONObject newJsonObject = JSON.parseObject(jsonString);

        //4. 获取JSONArray数据
        JSONArray jsonArray = newJsonObject.getJSONArray("data");

        //3. 输出
        System.out.println(jsonArray);
    }

    /**
     * 对象User输出格式
     *
     * User {
     *     id=1,
     *     name='刘德华'
     * }
     */
    public static void test1() {
        User u = new User(1,"刘德华");
        System.out.println(u.toString());
    }

    /**
     * 对象User的json格式表示
     *
     * {
     *     "id":1,
     *     "name":"刘德华"
     * }
     */
    public static void test2() {
        User u = new User(1,"刘德华");
        System.out.println(JSON.toJSON(u));
        System.out.println(JSON.toJSONString(u));
    }

    /**
     * List输出格式
     *
     * [
     * 	User{
     * 		id=1,
     * 		name='刘德华'
     *        },
     * 	User{
     * 		id=1,
     * 		name='林峰'
     *    },
     * 	User{
     * 		id=1,
     * 		name='毛不易'
     *    }
     * ]
     */
    public static void test6() {
        //1. 创建User集合
        List<User> users = new ArrayList<>();
        User u1 = new User(1,"刘德华");
        User u2 = new User(1,"林峰");
        User u3 = new User(1,"毛不易");
        users.add(u1);
        users.add(u2);
        users.add(u3);

        //2. 输出List
        System.out.println(users);
    }

    /**
     * List转json 字符串的输出格式
     *
     * [
     *     {
     *         "id":1,
     *         "name":"刘德华"
     *     },
     *     {
     *         "id":1,
     *         "name":"林峰"
     *     },
     *     {
     *         "id":1,
     *         "name":"毛不易"
     *     }
     * ]
     */
    public static void test9() {
        //1. 创建User集合
        List<User> users = new ArrayList<>();
        User u1 = new User(1,"刘德华");
        User u2 = new User(1,"林峰");
        User u3 = new User(1,"毛不易");
        users.add(u1);
        users.add(u2);
        users.add(u3);

        //2. jsonString
        String jsonString = JSONArray.toJSONString(users);
        System.out.println("方式一:" + jsonString);

        jsonString = JSON.toJSONString(users);
        System.out.println("方式二:" + jsonString);
    }

    /**
     * JSONObject以json字符串的形式输出User数组
     *
     * {
     *     "msg":"success",
     *     "code":"200",
     *     "data":[
     *         {
     *             "id":1,
     *             "name":"刘德华"
     *         },
     *         {
     *             "id":1,
     *             "name":"林峰"
     *         },
     *         {
     *             "id":1,
     *             "name":"毛不易"
     *         }]
     * }
     */
    public static void test7() {
        //1. 创建User集合
        List<User> users = new ArrayList<>();
        User u1 = new User(1,"刘德华");
        User u2 = new User(1,"林峰");
        User u3 = new User(1,"毛不易");
        users.add(u1);
        users.add(u2);
        users.add(u3);

        //2. 创建JSONObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "200");
        jsonObject.put("msg", "success");
        jsonObject.put("data", users);
        System.out.println("jsonObject = " + jsonObject);

        //3. 创建jsonString
        String jsonString = JSON.toJSONString(jsonObject);
        System.out.println(jsonString);
    }

    /**
     * User对象在JSONArray中的输出格式
     *
     * [
     *     {
     *         "name":"刘德华",
     *         "id":1
     *     },
     *     {
     *         "name":"林峰",
     *         "id":1
     *     },
     *     {
     *         "name":"毛不易",
     *         "id":1
     *     }
     * ]
     */
    public static void test8() {
        //1. 创建User集合
        List<User> users = new ArrayList<>();
        User u1 = new User(1,"刘德华");
        User u2 = new User(1,"林峰");
        User u3 = new User(1,"毛不易");
        users.add(u1);
        users.add(u2);
        users.add(u3);

        //2. 创建JSONObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "200");
        jsonObject.put("msg", "success");
        jsonObject.put("data", users);

        //3. 创建jsonString
        String jsonString = JSON.toJSONString(jsonObject);

        //4. 解析
        JSONObject newJsonObject = JSON.parseObject(jsonString);

        //5. 创建JSONArray
        JSONArray jsonArray = newJsonObject.getJSONArray("data");

        //6. 输出
        System.out.println(jsonArray);
    }

    public static void main(String[] args) {

        System.out.println("\nint数组输出格式:");
        test3();

        System.out.println("\nJSONObject以jsonString形式把存储的int数组输出格式: ");
        test4();

        System.out.println("\nJSONArray中存储的int数组输出格式: ");
        test5();

        System.out.println("对象User输出格式: ");
        test1();

        System.out.println("\n对象User的json格式表示: ");
        test2();

        System.out.println("\nList输出格式: ");
        test6();

        System.out.println("\nJSONObject以json字符串的形式输出User数组: ");
        test7();

        System.out.println("\nUser对象在JSONArray中的输出格式: ");
        test8();

        System.out.println("\nList转json 字符串的输出格式: ");
        test9();
    }
}

User.java

package com.example.demo.entity;

public class User {
    private Integer id;
    private String name;

    public User() {
    }

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

输出

int数组输出格式:
[1, 2, 3]

JSONObject以jsonString形式把存储的int数组输出格式: 
{"msg":"success","code":"200","data":[1,2,3]}

JSONArray中存储的int数组输出格式: 
[1,2,3]
对象User输出格式: 
User{id=1, name='刘德华'}

对象User的json格式表示: 
{"name":"刘德华","id":1}
{"id":1,"name":"刘德华"}

List<User>输出格式: 
[User{id=1, name='刘德华'}, User{id=1, name='林峰'}, User{id=1, name='毛不易'}]

JSONObject以json字符串的形式输出User数组: 
jsonObject = {"msg":"success","code":"200","data":[{"id":1,"name":"刘德华"},{"id":1,"name":"林峰"},{"id":1,"name":"毛不易"}]}
{"msg":"success","code":"200","data":[{"id":1,"name":"刘德华"},{"id":1,"name":"林峰"},{"id":1,"name":"毛不易"}]}

User对象在JSONArray中的输出格式: 
[{"name":"刘德华","id":1},{"name":"林峰","id":1},{"name":"毛不易","id":1}]

List<User>转json 字符串的输出格式: 
方式一:[{"id":1,"name":"刘德华"},{"id":1,"name":"林峰"},{"id":1,"name":"毛不易"}]
方式二:[{"id":1,"name":"刘德华"},{"id":1,"name":"林峰"},{"id":1,"name":"毛不易"}]

你可能感兴趣的:(java)