fastjson

https://repo1.maven.org/maven2/com/alibaba/fastjson/1.2.24/

image-20220608221004798

image-20220608221055172

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.24</version>
</dependency>
</dependencies>

image-20220608213750520

Persion.java

public class Persion {
public String name;
private int age;

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

public String getName() {
// System.out.println("getName执行了");
return name;
}

public void setName(String name) {
// System.out.println("setName执行了");
this.name = name;
}

public int getAge() {
// System.out.println("getAge执行了");
return age;
}

public void setAge(int age) {
// System.out.println("setAge执行了");
this.age = age;
}

public Persion() {
}

public Persion(String name, int age) {
this.name = name;
this.age = age;
}
}

Fastjson.java

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.sql.SQLOutput;

public class Fastjson {
public static void main(String[] args) throws Exception{
// new一个persion对象
Persion persion = new Persion("wan", 18);
// 将persion对象使用toJSONString转换成字符串s
String s = JSONObject.toJSONString(persion);
// 打印s
System.out.println("s:" + s);

String s1 = JSONObject.toJSONString(persion, SerializerFeature.WriteClassName);
System.out.println("s1:"+ s1);




}
}

image-20220608214512647

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.sql.SQLOutput;

public class Fastjson {
public static void main(String[] args) throws Exception{
// 把对象准换成字符串
// new一个persion对象
Persion persion = new Persion("wan", 18);
// 将persion对象使用toJSONString转换成字符串s
String noType = JSONObject.toJSONString(persion);
// 打印s
System.out.println("noType:" + noType);
// noType:{"age":18,"name":"wan"}
String type = JSONObject.toJSONString(persion, SerializerFeature.WriteClassName);
System.out.println("type:"+ type);
// 其中@type表示允许用户在反序列化数据中通过@type指定反序列化的class类型
// s1:{"@type":"Persion","age":18,"name":"wan"}



// 将字符串转换成对象
String noType1 = "{\"age\":18,\"name\":\"wan\"}";
// 什么都不执行
Object noType1parse = JSONObject.parse(noType1);
System.out.println(noType1parse);
// 什么都不执行
JSONObject noType1Object = JSONObject.parseObject(noType1);
System.out.println(noType1Object);

String type1 = "{\"@type\":\"Persion\",\"age\":18,\"name\":\"wan\"}";
// 执行set 和 toString
Object type2parse = JSONObject.parse(type1);
System.out.println(type2parse);
// 执行set 和 get
JSONObject type2Object = JSONObject.parseObject(type1);
System.out.println(type2Object);


}
}

image-20220608220126642

image-20220608221223780

这里调了parse

image-20220608221309272

image-20220608221353089

这里多了一个转换成json对象

image-20220608221643420

image-20220608222242705

image-20220608222318239