Json串和java对象进行转时

json-lib-xxx.jar
ezmorph-xxx.jar  //=============>依赖包

JsonConfig config = new JsonConfig();//有选择性的过滤掉一些属性值

 JSONUtils.getMorpherRegistry().registerMorpher( new DateMorpher(new String[] { "yyyy-MM-dd" }));//注册一个json转为java.util.date的日期格式

 JSONObject o = JSONObject.fromObject(jsonString, config);
  if (clazz == null) {
   return (T) JSONObject.toBean(o);
  } else {
   return (T) JSONObject.toBean(o, clazz);
  }

=====================================================================================================

public static void main(String[] args) {
Map map=new HashMap();
map.put("我","妹");
map.put("擦","哇");
map.put("你","呀");
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
}

輸出的結果 {"我":"妹","擦":"哇","你":"呀"}

然后测试toBean方法的类
import net.sf.json.JSONObject;

public class ToBeanTest {

public static void main(String[] args) {

String json = "{id:'1001',name:'张三',age:'22'}";
Student stu = new Student();
JSONObject obj = JSONObject.fromObject(json);
stu = (Student)JSONObject.toBean(obj, Student.class);
System.out.println(stu);
}

}
输出结果为1001, 张三, 22

原文地址:https://www.cnblogs.com/mingtian521/p/3835672.html