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