JSON-lib is a Java class package used to convert beans, maps, and XML into JSON and convert JSON back to bean and dynabean.
: Http://json-lib.sourceforge.net/
The third-party package is also required:
Org. Apache. commons (version 3.2 or later)
Org. Apache. oro
Net. SF. ezmorph (ezmorph-1.0.4.jar)
Nu. XOM
1. List
Java code
- Boolean[] Boolarray =
New
Boolean[] {True,False,True};
- Jsonarray jsonarray1 = jsonarray. fromobject (boolarray );
- System. Out. println (jsonarray1 );
- // Prints [true, false, true]
- List list =NewArraylist ();
- List. Add ("first ");
- List. Add ("second ");
- Jsonarray jsonarray2 = jsonarray. fromobject (list );
- System. Out. println (jsonarray2 );
- // Prints ["first", "second"]
- Jsonarray jsonarray3 = jsonarray. fromobject ("['json', 'is', 'easy']");
- System. Out. println (jsonarray3 );
- // Prints ["JSON", "is", "easy"]
boolean[] boolArray = new boolean[]{true,false,true};JSONArray jsonArray1 = JSONArray.fromObject( boolArray );System.out.println( jsonArray1 );// prints [true,false,true]List list = new ArrayList();list.add( "first" );list.add( "second" );JSONArray jsonArray2 = JSONArray.fromObject( list );System.out.println( jsonArray2 );// prints ["first","second"]JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );System.out.println( jsonArray3 );// prints ["json","is","easy"]
2. Map
Java code
- Map map =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) {return this. Arr [I];}");
- Jsonobject JSON = jsonobject. fromobject (MAP );
- System. Out. println (JSON );
- // {"Func": function (I) {return this. arr [I] ;}, "arr": ["A", "B"], "int": 1, "name": "JSON", "bool ": true}
Map map = new HashMap();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]; }" );JSONObject json = JSONObject.fromObject( map );System.out.println( json );//{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
3. Bean
Java code
- /**
- * Bean. Java
- Private string name = "JSON ";
- Private int pojoid = 1;
- Private char [] Options = new char [] {'A', 'F '};
- Private string func1 = "function (I) {return this. Options [I];}";
- Private jsonfunction func2 = new jsonfunction (New String [] {"I"}, "return this. Options [I];");
- */
- Jsonobject = jsonobject. fromobject (NewJsonbean ());
- System. Out. println (jsonobject );
- // {"Func1": function (I) {return this. options [I];}, "pojoid": 1, "name": "JSON", "options": ["A", "F"], "func2 ": function (I) {return this. options [I] ;}}
/*** Bean.javaprivate String name = "json";private int pojoId = 1;private char[] options = new char[]{'a','f'};private String func1 = "function(i){ return this.options[i]; }";private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");*/JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );System.out.println( jsonObject );//{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
4. Beans
Java code
- /**
- * Private int row;
- Private int Col;
- Private string value;
- *
- */
- List list =NewArraylist ();
- Jsonbean2 jb1 =NewJsonbean2 ();
- Jb1.setcol (1 );
- Jb1.setrow (1 );
- Jb1.setvalue ("XX ");
- Jsonbean2 jb2 =NewJsonbean2 ();
- Jb2.setcol (2 );
- Jb2.setrow (2 );
- Jb2.setvalue ("");
- List. Add (jb1 );
- List. Add (jb2 );
- Jsonarray ja = jsonarray. fromobject (list );
- System. Out. println (JA. tostring ());
- // [{"Value": "XX", "row": 1, "col": 1 },{ "value": "", "row": 2, "col": 2}]
/*** private int row ;private int col ;private String value ;**/List list = new ArrayList();JsonBean2 jb1 = new JsonBean2();jb1.setCol(1);jb1.setRow(1);jb1.setValue("xx");JsonBean2 jb2 = new JsonBean2();jb2.setCol(2);jb2.setRow(2);jb2.setValue("");list.add(jb1);list.add(jb2);JSONArray ja = JSONArray.fromObject(list);System.out.println( ja.toString() );//[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
5. string to Bean
Java code
- String JSON = "{name = \" JSON \ ", bool: True, INT: 1, double: 2.2, FUNC: function (a) {return a ;}, array: [1, 2]} ";
- Jsonobject = jsonobject. fromstring (JSON );
- Object bean = jsonobject. tobean (jsonobject );
- Assertequals (jsonobject. Get ("name"), propertyutils. getproperty (bean,
"Name "));
- Assertequals (jsonobject. Get ("bool"), propertyutils. getproperty (bean,
"Bool "));
- Assertequals (jsonobject. Get ("int"), propertyutils. getproperty (bean,
"Int "));
- Assertequals (jsonobject. Get ("double"), propertyutils. getproperty (bean,
"Double "));
- Assertequals (jsonobject. Get ("func"), propertyutils. getproperty (bean,
"Func "));
- List expected = jsonarray. tolist (jsonobject. getjsonarray ("array "));
- Assertequals (expected, (list) propertyutils. getproperty (bean,
"Array "));
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";JSONObject jsonObject = JSONObject.fromString(json);Object bean = JSONObject.toBean( jsonObject );assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
Java code
- String JSON = "{\" value \ ": \" XX \ ", \" Row \ ": 1, \" Col \ ": 1 }";
- Jsonobject = jsonobject. fromstring (JSON );
- Jsonbean2 bean = (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 ());
String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";JSONObject jsonObject = JSONObject.fromString(json);JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) );assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );assertEquals( jsonObject.get( "value" ), bean.getValue() );
6 JSON to XML
1)
Jsonobject JSON = new jsonobject (true );
String xml = xmlserializer. Write (JSON );
<O class = "object" null = "true">
2)
Jsonobject JSON = jsonobject. fromobject ("{\" Name \ ": \" JSON \ ", \" bool \ ": True, \" int \ ": 1 }");
String xml = xmlserializer. Write (JSON );
<O class = "object">
<Name type = "string"> JSON </Name>
<Bool type = "Boolean"> true </bool>
<Int type = "Number"> 1 </int>
</O>
<O class = "object">
<Name type = "string"> JSON </Name>
<Bool type = "Boolean"> true </bool>
<Int type = "Number"> 1 </int>
</O>
3)
Jsonarray JSON = jsonarray. fromobject ("[1, 2, 3]");
String xml = xmlserializer. Write (JSON );
<A class = "array">
<E type = "Number"> 1 </E>
<E type = "Number"> 2 </E>
<E type = "Number"> 3 </E>
</A>
7. XML to JSON
<A class = "array">
<E type = "function" Params = "I, j">
Return matrix [I] [J];
</E>
</A>
<A class = "array">
<E type = "function" Params = "I, j">
Return matrix [I] [J];
</E>
</A>
Jsonarray JSON = (jsonarray) xmlserializer. Read (XML );
System. Out. println (JSON );
// Prints [function (I, j) {return matrix [I] [J];}]