解析json数据,将json转为实体类
public class TestFASTJSON { public static void jsonToT(){ String param = "{\"username\":\"17788332570\",\"pwd\":\"123456\"}"; RegisterParam registerParam = JSONObject.parseObject(param, RegisterParam.class); System.out.println(registerParam); } public static void main(String[] args) { jsonToT(); } }
RegisterParam
public class RegisterParam { private String username; private String pwd; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "RegisterParam{" + "username='" + username + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
需要jar
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency>