可以使用json-lib来序列化对象
依赖的jar包:
如何使用json-lib来序列化java对象呢? 实例:
- @Test
- public void test_serialize(){
- Class2 c=new Class2();
- List<Student>students=new ArrayList<Student>();
- Student student=new Student();
- Map<String, Object> attribute =new HashMap<String, Object>();
- attribute.put("p1", "v1");
- attribute.put("p2", "v2");
- student.setAttribute(attribute);
- students.add(student);
- c.setStudents(students);
- c.setClassName("计算机0705");
- JSONObject js = JSONObject.fromObject(c);
- System.out.println(js.toString());
- }
运行结果如下:
{"classAttribute":null,"className":"计算机0705","count":0,"students":[{"addrr":null,"age":0,"attribute":{"p2":"v2","p1":"v1"},"hobby":"","name":""}]}
实例:
- @Test
- public void test_serialize2(){
- Class2 c=new Class2();
- List<Student>students=new ArrayList<Student>();
- Student student=new Student();
- Map<String, Object> attribute =new HashMap<String, Object>();
- attribute.put("p1", "v1");
- attribute.put("p2", "v2");
- student.setAttribute(attribute);
- students.add(student);
- c.setStudents(students);
- c.setClassName("计算机0705");
-
- Map<String, Object> classAttribute =new HashMap<String, Object>();
- classAttribute.put("pp1", "vv1");
- classAttribute.put("pp2", "vv2");
-
-
- Teacher t=new Teacher();
- t.setName("熊应标");
- t.setTitle("语文老师");
-
- c.setClassAttribute(classAttribute);
-
-
-
-
- JSONObject js = JSONObject.fromObject(c);
- System.out.println(js.toString());
- }
运行结果如下:
{"classAttribute":{"pp1":"vv1","pp2":"vv2"},"className":"计算机0705","count":0,"students":[{"addrr":null,"age":0,"attribute":{"p2":"v2","p1":"v1"},"hobby":"","name":""}]}
如何使用json-lib反序列化(把string转化为Java对象)?
参考:http://hw1287789687.iteye.com/admin/blogs/1993048
实例:
- @Test
- public void test_reserialize(){
-
- String jsonInput="{\"classAttribute\":{\"pp1\":\"vv1\",\"pp2\":\"vv2\"},\"className\":\"计算机0705\",\"count\":0,\"students\":[{\"addrr\":null,\"age\":0,\"attribute\":{\"p2\":\"v2\",\"p1\":\"v1\"},\"hobby\":\"\",\"name\":\"\"}]}";
- JSONObject js = JSONObject.fromObject(jsonInput);
-
- JsonConfig jsonConfig = new JsonConfig();
-
- jsonConfig.setRootClass(Class2.class);
- Map<String, Class> classMap = new HashMap<String, Class>();
- classMap.put("students", Student.class);
- jsonConfig.setClassMap(classMap);
-
-
-
- Class2 one = (Class2) JSONObject.toBean(js, jsonConfig);
- System.out.println(one.getClassName());
- Map<String, Object> attribute =one.getStudents().get(0).getAttribute();
- System.out.println(attribute);
-
- }
运行结果:
计算机0705
{p2=v2, p1=v1}
序列化时如何删除属性值为null的属性
参考:
下面的事不合要求的:
{"addrr":{"country":"中国","state":"湖北省","street":"清河"},"age":25,"hobby":"","name":"黄威"} |
hobby的值为空,应该被过滤掉的。
解决方法:
JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { publicboolean apply(Object source, String name, Object value) { // System.out.println(name); // System.out.println(value); if (value == null || (value instanceof String) && ((String) value).equals("")) { returntrue; } returnfalse; } }); JSONObject js = JSONObject.fromObject(student, jsonConfig); |
参考:http://hw1287789687.iteye.com/blog/1997956