引入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 {
public static void test3() {
int[] nums = {1,2,3};
System.out.println(Arrays.toString(nums));
}
public static void test4() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "200");
jsonObject.put("msg", "success");
int[] nums = {1,2,3};
jsonObject.put("data", nums);
String jsonString = JSON.toJSONString(jsonObject);
System.out.println(jsonString);
}
public static void test5() {
int[] nums = {1,2,3};
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "200");
jsonObject.put("msg", "success");
jsonObject.put("data", nums);
String jsonString = JSON.toJSONString(jsonObject);
JSONObject newJsonObject = JSON.parseObject(jsonString);
JSONArray jsonArray = newJsonObject.getJSONArray("data");
System.out.println(jsonArray);
}
public static void test1() {
User u = new User(1,"刘德华");
System.out.println(u.toString());
}
public static void test2() {
User u = new User(1,"刘德华");
System.out.println(JSON.toJSON(u));
System.out.println(JSON.toJSONString(u));
}
public static void test6() {
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);
System.out.println(users);
}
public static void test9() {
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);
String jsonString = JSONArray.toJSONString(users);
System.out.println("方式一:" + jsonString);
jsonString = JSON.toJSONString(users);
System.out.println("方式二:" + jsonString);
}
public static void test7() {
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);
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "200");
jsonObject.put("msg", "success");
jsonObject.put("data", users);
System.out.println("jsonObject = " + jsonObject);
String jsonString = JSON.toJSONString(jsonObject);
System.out.println(jsonString);
}
public static void test8() {
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);
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "200");
jsonObject.put("msg", "success");
jsonObject.put("data", users);
String jsonString = JSON.toJSONString(jsonObject);
JSONObject newJsonObject = JSON.parseObject(jsonString);
JSONArray jsonArray = newJsonObject.getJSONArray("data");
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":"毛不易"}]