fastjson反序列

文章展示了如何使用阿里巴巴的Fastjson库进行Java对象到JSON字符串的转换,以及如何处理类型信息以支持反序列化。通过`toJSONString`方法,可以将Persion类的对象转换为JSON字符串,并通过`parse`和`parseObject`方法将字符串回溯为对象。在处理过程中,`SerializerFeature.WriteClassName`特征用于保留类名信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值