想要使用JSONUtil.parse将java对象转为json格式,但是一直为空,代码如下
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格式
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;
}
}