技术开发 频道

JSON lib框架转换JSON XML不再困难!

  3、 将Map集合转换成JSON对象

/**
* <b>function:</b>转Java Map对象到JSON
*
@author hoojo
* @createDate Nov 28, 2010 2:37:35 PM
*/
@Test
public void writeMap2JSON() {
Map
<String, Object> map = new HashMap<String, Object>();
map.put(
"A", bean);

bean.setName(
"jack");
map.put(
"B", bean);
map.put(
"name", "json");
map.put(
"bool", Boolean.TRUE);
map.put(
"int", new Integer(1));
map.put(
"arr", new String[] { "a", "b" });
map.put(
"func", "function(i){ return this.arr[i]; }");
fail(
"==============Java Map >>> JSON Object==================");
fail(JSONObject.fromObject(map).toString());
fail(
"==============Java Map >>> JSON Array ==================");
fail(JSONArray.fromObject(map).toString());
fail(
"==============Java Map >>> JSON Object==================");
fail(JSONSerializer.toJSON(map).toString());
}

 

  上面的Map集合有JavaBean、String、Boolean、Integer、以及Array和js的function函数的字符串。

  运行上面的程序,结果如下:

==============Java Map >>> JSON Object==================
{
"arr":["a","b"],"A":{"address":"address","birthday":{"birthday":"2010-11-22"},"email":"email","id":1,"name":"jack"},"int":1,
"B":{"address":"address","birthday":{"birthday":"2010-11-22"},"email":"email","id":1,"name":"jack"},"name":"json",
"func":function(i){ return this.arr[i]; },"bool":true}
==============Java Map >>> JSON Array ==================
[{
"arr":["a","b"],"A":{"address":"address","birthday":{"birthday":"2010-11-22"},"email":"email","id":1,"name":"jack"},"int":1,
"B":{"address":"address","birthday":{"birthday":"2010-11-22"},"email":"email","id":1,"name":"jack"},"name":"json",
"func":function(i){ return this.arr[i]; },"bool":true}]
==============Java Map >>> JSON Object==================
{
"arr":["a","b"],"A":{"address":"address","birthday":{"birthday":"2010-11-22"},"email":"email","id":1,"name":"jack"},"int":1,
"B":{"address":"address","birthday":{"birthday":"2010-11-22"},"email":"email","id":1,"name":"jack"},"name":"json",
"func":function(i){ return this.arr[i]; },"bool":true}

 

  4、 将更多类型转换成JSON

/**
* <b>function:</b> 转换更多数组类型到JSON
*
@author hoojo
* @createDate Nov 28, 2010 2:39:19 PM
*/
@Test
public void writeObject2JSON() {
String[] sa
= {"a", "b", "c"};
fail(
"==============Java StringArray >>> JSON Array ==================");
fail(JSONArray.fromObject(sa).toString());
fail(JSONSerializer.toJSON(sa).toString());
fail(
"==============Java boolean Array >>> JSON Array ==================");
boolean[] bo = { true, false, true };
fail(JSONArray.fromObject(bo).toString());
fail(JSONSerializer.toJSON(bo).toString());
Object[] o
= { 1, "a", true, 'A', sa, bo };
fail(
"==============Java Object Array >>> JSON Array ==================");
fail(JSONArray.fromObject(o).toString());
fail(JSONSerializer.toJSON(o).toString());
fail(
"==============Java String >>> JSON ==================");
fail(JSONArray.fromObject(
"['json','is','easy']").toString());
fail(JSONObject.fromObject(
"{'json':'is easy'}").toString());
fail(JSONSerializer.toJSON(
"['json','is','easy']").toString());
fail(
"==============Java JSONObject >>> JSON ==================");
jsonObject
= new JSONObject()
.element(
"string", "JSON")
.element(
"integer", "1")
.element(
"double", "2.0")
.element(
"boolean", "true");
fail(JSONSerializer.toJSON(jsonObject).toString());
fail(
"==============Java JSONArray >>> JSON ==================");
jsonArray
= new JSONArray()
.element(
"JSON" )
.element(
"1" )
.element(
"2.0" )
.element(
"true" );
fail(JSONSerializer.toJSON(jsonArray).toString());
fail(
"==============Java JSONArray JsonConfig#setArrayMode >>> JSON ==================");
List input
= new ArrayList();
input.add(
"JSON");
input.add(
"1");
input.add(
"2.0");
input.add(
"true");
JSONArray jsonArray
= (JSONArray) JSONSerializer.toJSON( input );
JsonConfig jsonConfig
= new JsonConfig();
jsonConfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY );
Object[] output
= (Object[]) JSONSerializer.toJava(jsonArray, jsonConfig);
System.out.println(output[
0]);
fail(
"==============Java JSONFunction >>> JSON ==================");
String str
= "{'func': function( param ){ doSomethingWithParam(param); }}";
JSONObject jsonObject
= (JSONObject) JSONSerializer.toJSON(str);
JSONFunction func
= (JSONFunction) jsonObject.get("func");
fail(func.getParams()[
0]);
fail(func.getText() );
}
0
相关文章