一、目标
把类的对象存储到字符串可存储
二、类定义
public interface JsonInterface {
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class JsonVo implements JsonInterface {
private String id;
private String name;
public JsonVo(String id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return String.format("id:%s |||| name:%s", id, name);
}
}
}
三、测试
public void test() {
JsonInterface tmp = new JsonVo("id1", "name1");
String json = JSON.toJSONString(tmp);
System.out.println(json);
JsonInterface jsonVo = JSON.parseObject(json, JsonVo.class);
System.out.println(jsonVo);