字符串转成JSON对象,再把JSON对象转成实体类
实体类:
import lombok.Data;
/**
* 实体类:TestEntity
* author:oowangfanoo
* time:2021-02-24
*/
@Data
public class TestEntity {
/**
* 记录唯一标识ID
*/
private String id;
/**
* 用户名
*/
private String name;
/**
* 年龄
*/
private String sex;
}
执行代码:
public static void main(String[] args) {
String testParam = "{\"id\":\"1001\",\"name\":\"oowangfanoo\",\"sex\":\"30\"}";
TestEntity testEntity = JSON.toJavaObject(JSONObject.parseObject(testParam),TestEntity.class) ;
System.out.println(testEntity.toString());
}
注:JSONObject的包为com.alibaba.fastjson;