欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

将String转化成java对象

程序员文章站 2022-03-31 09:35:50
...
import com.alibaba.fastjson.JSONObject;
public void   getObject () throws NoSuchMethodException {
        JSONObject object1 = new JSONObject();
        object1.put("openid","123");
        object1.put("mobile","131313020");
        object1.put("status","1");
        List<String> p = new ArrayList<>();
        p.add("hello");
        p.add("hi");
        object1.put("hobbys",p);
        System.out.println("object1:"+object1);
        JWJTest parse = (JWJTest)JSONObject.parseObject(object1.toString(), JWJTest.class);
        System.out.println("parse:"+parse);
        List<String> hobbys = parse.getHobbys();
        System.out.println("hobbys:"+hobbys);
}
@Data
public class JWJTest {
    String mobile;
    String openid;
    String status;
    List<String> hobbys;
}