Examples on the official website
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"]
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}
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] ;}}
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}]
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 "));
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 ());
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];}]