JSONUtil.parse将java对象转为json时,需要在java对象中设置get、set方法

想要使用JSONUtil.parse将java对象转为json格式,但是一直为空,代码如下

java 复制代码
public class MyTest {
    public static void main(String[] args) {

        Test3<String> test3 = new Test3<>("2","hhhhhhaaa");

        System.out.println(JSONUtil.parse(test3));
    }
}

class Test3<T>{
    String name;
    T msg;

    Test3(String name,T msg){
        this.name = name;
        this.msg = msg;
    }
}

后来经过指点,需要加入set和get方法才能转为json格式

java 复制代码
public class MyTest {
    public static void main(String[] args) {

        Test3<String> test3 = new Test3<>("2","hhhhhhaaa");

        System.out.println(JSONUtil.parse(test3));
    }
}

class Test3<T>{
    String name;
    T msg;

    Test3(String name,T msg){
        this.name = name;
        this.msg = msg;
    }

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

    public String getName(){
        return this.name;
    }

    public void setMsg(T msg){
        this.msg = msg;
    }

    public T getMsg(){
        return this.msg;
    }


}
相关推荐
Derek_Smart1 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
曲幽2 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
NE_STOP2 小时前
MyBatis-mybatis入门与增删改查
java
孟陬5 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌5 小时前
一站式了解四种限流算法
java·后端·go
华仔啊6 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java
敏编程6 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪6 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
也些宝6 小时前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java