下载地址: http://json-lib.sourceforge.net/
还要需要的第3方包:
org.apache.commons(3.2以上版本)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom
1、List
- boolean[]boolArray=newboolean[]{true,false,true};
- JSONArrayjsonArray1=JSONArray.fromObject(boolArray);
- System.out.println(jsonArray1);
- //prints[true,false,true]
- Listlist=newArrayList();
- list.add("first");
- list.add("second");
- JSONArrayjsonArray2=JSONArray.fromObject(list);
- System.out.println(jsonArray2);
- //prints["first","second"]
- JSONArrayjsonArray3=JSONArray.fromObject("['json','is','easy']");
- System.out.println(jsonArray3);
- //prints["json","is","easy"]
- boolean[]boolArray=newboolean[]{true,"easy"]
2、Map
- Mapmap=newHashMap();
- map.put("name","json");
- map.put("bool",Boolean.TRUE);
- map.put("int",newInteger(1));
- map.put("arr",newString[]{"a","b"});
- map.put("func","function(i){returnthis.arr[i];}");
- JSONObjectjson=JSONObject.fromObject(map);
- System.out.println(json);
- //{"func":function(i){returnthis.arr[i];},"arr":["a","b"],"int":1,"name":"json","bool":true}
- Mapmap=newHashMap();
- map.put("name","bool":true}
3、BEAN
- /**
- *Bean.java
- privateStringname="json";
- privateintpojoId=1;
- privatechar[]options=newchar[]{'a','f'};
- privateStringfunc1="function(i){returnthis.options[i];}";
- privateJSONFunctionfunc2=newJSONFunction(newString[]{"i"},"returnthis.options[i];");
- */
- JSONObjectjsonObject=JSONObject.fromObject(newJsonBean());
- System.out.println(jsonObject);
- //{"func1":function(i){returnthis.options[i];},"pojoId":1,"options":["a","f"],"func2":function(i){returnthis.options[i];}}
- /**
- *Bean.java
- privateStringname="json";
- privateintpojoId=1;
- privatechar[]options=newchar[]{'a',"func2":function(i){returnthis.options[i];}}
4、BEANS
- /**
- *privateintrow;
- privateintcol;
- privateStringvalue;
- *
- */
- Listlist=newArrayList();
- JsonBean2jb1=newJsonBean2();
- jb1.setCol(1);
- jb1.setRow(1);
- jb1.setValue("xx");
- JsonBean2jb2=newJsonBean2();
- jb2.setCol(2);
- jb2.setRow(2);
- jb2.setValue("");
- list.add(jb1);
- list.add(jb2);
- JSONArrayja=JSONArray.fromObject(list);
- System.out.println(ja.toString());
- //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
- /**
- *privateintrow;
- privateintcol;
- privateStringvalue;
- *
- */
- Listlist=newArrayList();
- JsonBean2jb1=newJsonBean2();
- jb1.setCol(1);
- jb1.setRow(1);
- jb1.setValue("xx");
- JsonBean2jb2=newJsonBean2();
- jb2.setCol(2);
- jb2.setRow(2);
- jb2.setValue("");
- list.add(jb1);
- list.add(jb2);
- JSONArrayja=JSONArray.fromObject(list);
- System.out.println(ja.toString());
- //[{"value":"xx","col":2}]
5、String to bean
- Stringjson="{name=\"json\",bool:true,int:1,double:2.2,func:function(a){returna;},array:[1,2]}";
- JSONObjectjsonObject=JSONObject.fromString(json);
- Objectbean=JSONObject.toBean(jsonObject);
- assertEquals(jsonObject.get("name"),PropertyUtils.getProperty(bean,"name"));
- assertEquals(jsonObject.get("bool"),"bool"));
- assertEquals(jsonObject.get("int"),"int"));
- assertEquals(jsonObject.get("double"),"double"));
- assertEquals(jsonObject.get("func"),"func"));
- Listexpected=JSONArray.toList(jsonObject.getJSONArray("array"));
- assertEquals(expected,(List)PropertyUtils.getProperty(bean,"array"));
- Stringjson="{name=\"json\","array"));
- Stringjson="{\"value\":\"xx\",\"row\":1,\"col\":1}";
- JSONObjectjsonObject=JSONObject.fromString(json);
- JsonBean2bean=(JsonBean2)JSONObject.toBean(jsonObject,JsonBean2.class);
- assertEquals(jsonObject.get("col"),newInteger(bean.getCol()));
- assertEquals(jsonObject.get("row"),newInteger(bean.getRow()));
- assertEquals(jsonObject.get("value"),bean.getValue());
- Stringjson="{\"value\":\"xx\",bean.getValue());