运行后结果如下:
==============Java StringArray >>> JSON Array ==================
["a","b","c"]
["a","b","c"]
==============Java boolean Array >>> JSON Array ==================
[true,false,true]
[true,false,true]
==============Java Object Array >>> JSON Array ==================
[1,"a",true,"A",["a","b","c"],[true,false,true]]
[1,"a",true,"A",["a","b","c"],[true,false,true]]
==============Java String >>> JSON ==================
["json","is","easy"]
{"json":"is easy"}
["json","is","easy"]
==============Java JSONObject >>> JSON ==================
{"string":"JSON","integer":"1","double":"2.0","boolean":"true"}
==============Java JSONArray >>> JSON ==================
["JSON","1","2.0","true"]
==============Java JSONArray JsonConfig#setArrayMode >>> JSON ==================
JSON
==============Java JSONFunction >>> JSON ==================
param
doSomethingWithParam(param);
["a","b","c"]
["a","b","c"]
==============Java boolean Array >>> JSON Array ==================
[true,false,true]
[true,false,true]
==============Java Object Array >>> JSON Array ==================
[1,"a",true,"A",["a","b","c"],[true,false,true]]
[1,"a",true,"A",["a","b","c"],[true,false,true]]
==============Java String >>> JSON ==================
["json","is","easy"]
{"json":"is easy"}
["json","is","easy"]
==============Java JSONObject >>> JSON ==================
{"string":"JSON","integer":"1","double":"2.0","boolean":"true"}
==============Java JSONArray >>> JSON ==================
["JSON","1","2.0","true"]
==============Java JSONArray JsonConfig#setArrayMode >>> JSON ==================
JSON
==============Java JSONFunction >>> JSON ==================
param
doSomethingWithParam(param);
这里还有一个JSONFunction的对象,可以转换JavaScript的function。可以获取方法参数和方法体。同时,还可以用JSONObject、JSONArray构建Java对象,完成Java对象到JSON字符串的转换。
三、 JSON对象反序列化成Java对象
1、 将json字符串转成Java对象
private String json = "{\"address\":\"chian\",\"birthday\":{\"birthday\":\"2010-11-22\"}," +
"\"email\":\"email@123.com\",\"id\":22,\"name\":\"tom\"}";
/**
* <b>function:</b>将json字符串转化为java对象
* @author hoojo
* @createDate Nov 28, 2010 3:01:16 PM
*/
@Test
public void readJSON2Bean() {
fail("==============JSON Object String >>> Java Bean ==================");
jsonObject = JSONObject.fromObject(json);
Student stu = (Student) JSONObject.toBean(jsonObject, Student.class);
fail(stu.toString());
}
"\"email\":\"email@123.com\",\"id\":22,\"name\":\"tom\"}";
/**
* <b>function:</b>将json字符串转化为java对象
* @author hoojo
* @createDate Nov 28, 2010 3:01:16 PM
*/
@Test
public void readJSON2Bean() {
fail("==============JSON Object String >>> Java Bean ==================");
jsonObject = JSONObject.fromObject(json);
Student stu = (Student) JSONObject.toBean(jsonObject, Student.class);
fail(stu.toString());
}
运行后,结果如下:
==============JSON Object String >>> Java Bean ==================
tom#22#chian#2010-11-22#email@123.com
tom#22#chian#2010-11-22#email@123.com
2、 将json字符串转换成动态Java对象(MorphDynaBean)
private String json = "{\"address\":\"chian\",\"birthday\":{\"birthday\":\"2010-11-22\"},"+
"\"email\":\"email@123.com\",\"id\":22,\"name\":\"tom\"}";
@Test
public void readJSON2DynaBean() {
try {
fail("==============JSON Object String >>> Java MorphDynaBean ==================");
JSON jo = JSONSerializer.toJSON(json);
Object o = JSONSerializer.toJava(jo);//MorphDynaBean
fail(PropertyUtils.getProperty(o, "address").toString());
jsonObject = JSONObject.fromObject(json);
fail(jsonObject.getString("email"));
o = JSONSerializer.toJava(jsonObject);//MorphDynaBean
fail(PropertyUtils.getProperty(o, "name").toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
"\"email\":\"email@123.com\",\"id\":22,\"name\":\"tom\"}";
@Test
public void readJSON2DynaBean() {
try {
fail("==============JSON Object String >>> Java MorphDynaBean ==================");
JSON jo = JSONSerializer.toJSON(json);
Object o = JSONSerializer.toJava(jo);//MorphDynaBean
fail(PropertyUtils.getProperty(o, "address").toString());
jsonObject = JSONObject.fromObject(json);
fail(jsonObject.getString("email"));
o = JSONSerializer.toJava(jsonObject);//MorphDynaBean
fail(PropertyUtils.getProperty(o, "name").toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}