用通俗易懂的方法将,所谓的json的Object形式就是一个花括号里面存放的如JavaMap的键值对,如:{name:’hoojo’, age: 24};
那么json的Array形式呢?
就是中括号,括起来的数组。如:[ ‘json’, true, 22];
如果你还想了解更多json方面的知识,请看:http://www.json.org/json-zh.html
除了上面的JSONArray、JSONObject可以将Java对象转换成JSON或是相反,将JSON字符串转换成Java对象,还有一个对象也可以完成上面的功能,它就是JSONSerializer;下面我们就来看看它们是怎么玩转Java对象和JSON的。
二、 Java对象序列化成JSON对象
1、 将JavaObject转换吃JSON字符串
在JsonlibTest中添加如下代码:
/*=========================Java Object >>>> JSON String ===========================*/
/**
* <b>function:</b>转Java Bean对象到JSON
* @author hoojo
* @createDate Nov 28, 2010 2:35:54 PM
*/
@Test
public void writeEntity2JSON() {
fail("==============Java Bean >>> JSON Object==================");
fail(JSONObject.fromObject(bean).toString());
fail("==============Java Bean >>> JSON Array==================");
fail(JSONArray.fromObject(bean).toString());//array会在最外层套上[]
fail("==============Java Bean >>> JSON Object ==================");
fail(JSONSerializer.toJSON(bean).toString());
fail("========================JsonConfig========================");
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Birthday.class, new JsonValueProcessor() {
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
if (value == null) {
return new Date();
}
return value;
}
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
fail("key:" + key);
return value + "##修改过的日期";
}
});
jsonObject = JSONObject.fromObject(bean, jsonConfig);
fail(jsonObject.toString());
Student student = (Student) JSONObject.toBean(jsonObject, Student.class);
fail(jsonObject.getString("birthday"));
fail(student.toString());
fail("#####################JsonPropertyFilter############################");
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
fail(source + "%%%" + name + "--" + value);
//忽略birthday属性
if (value != null && Birthday.class.isAssignableFrom(value.getClass())) {
return true;
}
return false;
}
});
fail(JSONObject.fromObject(bean, jsonConfig).toString());
fail("#################JavaPropertyFilter##################");
jsonConfig.setRootClass(Student.class);
jsonConfig.setJavaPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
fail(name + "@" + value + "#" + source);
if ("id".equals(name) || "email".equals(name)) {
value = name + "@@";
return true;
}
return false;
}
});
//jsonObject = JSONObject.fromObject(bean, jsonConfig);
//student = (Student) JSONObject.toBean(jsonObject, Student.class);
//fail(student.toString());
student = (Student) JSONObject.toBean(jsonObject, jsonConfig);
fail("Student:" + student.toString());
}
/**
* <b>function:</b>转Java Bean对象到JSON
* @author hoojo
* @createDate Nov 28, 2010 2:35:54 PM
*/
@Test
public void writeEntity2JSON() {
fail("==============Java Bean >>> JSON Object==================");
fail(JSONObject.fromObject(bean).toString());
fail("==============Java Bean >>> JSON Array==================");
fail(JSONArray.fromObject(bean).toString());//array会在最外层套上[]
fail("==============Java Bean >>> JSON Object ==================");
fail(JSONSerializer.toJSON(bean).toString());
fail("========================JsonConfig========================");
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Birthday.class, new JsonValueProcessor() {
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
if (value == null) {
return new Date();
}
return value;
}
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
fail("key:" + key);
return value + "##修改过的日期";
}
});
jsonObject = JSONObject.fromObject(bean, jsonConfig);
fail(jsonObject.toString());
Student student = (Student) JSONObject.toBean(jsonObject, Student.class);
fail(jsonObject.getString("birthday"));
fail(student.toString());
fail("#####################JsonPropertyFilter############################");
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
fail(source + "%%%" + name + "--" + value);
//忽略birthday属性
if (value != null && Birthday.class.isAssignableFrom(value.getClass())) {
return true;
}
return false;
}
});
fail(JSONObject.fromObject(bean, jsonConfig).toString());
fail("#################JavaPropertyFilter##################");
jsonConfig.setRootClass(Student.class);
jsonConfig.setJavaPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
fail(name + "@" + value + "#" + source);
if ("id".equals(name) || "email".equals(name)) {
value = name + "@@";
return true;
}
return false;
}
});
//jsonObject = JSONObject.fromObject(bean, jsonConfig);
//student = (Student) JSONObject.toBean(jsonObject, Student.class);
//fail(student.toString());
student = (Student) JSONObject.toBean(jsonObject, jsonConfig);
fail("Student:" + student.toString());
}