Java method for reading JSON data

Source: Internet
Author: User

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

Import net. SF. JSON. jsonarray;
Import net. SF. JSON. jsonobject;

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 = 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"]

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 = 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}

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 (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] ;}}

/**
* 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 (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 = 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}]

/**
* 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. 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"), new INTEGER (bean. getcol ()));
Assertequals (jsonobject. Get ("row"), new INTEGER (bean. getrow ()));
Assertequals (jsonobject. Get ("value"), Bean. getvalue ());

String JSON = "{/" value/":/" XX/",/" Row/": 1,/" COL/": 1 }";
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];}]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.