Conversion between JSON and bean and between arrays and collections

Source: Internet
Author: User

Jar packages dependent on JSON can be downloaded to jar114.com.
1. commons-beanutils.jar
2. commons-collections-3.2.1.jar
3. commons-lang-2.5.jar
4. commons-logging-1.1.1.jar
5. ezmorph-1.0.6.jar
6. json-lib-2.2.3-jdk15.jar


// General Usage
Jsonarray = jsonarray. fromobject (getlist ());
System. Out. println (jsonarray );

LConvert arrays and collections

Boolean [] boolarray = new Boolean [] {true, false, true };

Jsonarray = jsonarray. fromobject (boolarray );

System. Out. println (jsonarray );

Output: [true, false, true]

List list = new arraylist ();

List. Add ("first ");

List. Add ("second ");

Jsonarray = jsonarray. fromobject (list );

System. Out. println (jsonarray );

Output: ["first", "second"]

Jsonarray jsonarray3 = jsonarray. fromobject ("['json', 'is', 'easy']");

System. Out. println (jsonarray3 );

Output: ["JSON", "is", "'easy'"]

LConversion object

Convert Map

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 );

Output: ["name": "JSON", "bool": True, "int", 1, "arr": ["A", "B"], "func": function (I) {return this. arr [I];}]

Convert Bean

Mybean. Java

Public class mybean {

Private string name = "JSON ";

Private int pojoid = 1;

Private string func1 = "function (I) {return this. Options [I]}";

Private jsonfunction func2 = new jsonfunction (New String [] {"I"}, "return this. Options [I];");

// The get and set methods are as follows:

}

Mybean bean = new mybean ();

Jsonobject = jsonobject. fromobject (bean );

System. Out. println (jsonobject );

Output:

{"Func1": function (I) {return this. options [I]}, "pojoid": 1, "name": "JSON", "func2": function (I) {return this. options [I];}

From JSON to beans

// Convert to dynamic Bean

String myjson = "{name = \" JSON \ ", bool: True, INT: 1, double: 2.2, function: function (a) {return a ;}, array: [1, 2]} ";

Jsonobject json1 = jsonobject. fromstring (myjson );

Object bean1 = jsonobject. tobean (json1 );

XML generated by JSON

Jsonobject JSON = new jsonobject (true );

Xmlserializer = new xmlserializer ();

String xml = xmlserializer. Write (JSON );

System. Out. println ("XML:" + XML );

Output: XML: <? XML version = "1.0" encoding = "UTF-8"?>

<O null = "true"/>

Jsonobject json2 = jsonobject. fromobject ("{\" Name \ ": \" JSON \ ", \" bool \ ": True, \" int \ ": 1 }");

String xml2 = xmlserializer. Write (json2 );

System. Out. println ("xml2:" + xml2 );

Output: xml2: <? XML version = "1.0" encoding = "UTF-8"?>

<O> <bool type = "Boolean"> true </bool> <int type = "Number"> 1 </int> <name type = "string"> JSON </ name> </o>

Jsonarray json3 = jsonarray. fromobject ("[1, 2, 3]");

String xml3 = xmlserializer. Write (json3 );

System. Out. println ("xml3:" + xml3 );

Output: xml3: <? XML version = "1.0" encoding = "UTF-8"?>

<A> <E type = "Number"> 1 </E> <E type = "Number"> 2 </E> <E type = "Number"> 3 </ e> </a>

Http://json-lib.sourceforge.net/usage.html from:

From JSON to beans
String JSON = "{name = \" JSON \ ", bool: True, INT: 1, double: 2.2, FUNC: function (a) {return a ;}, array: [1, 2]} ";

Jsonobject = jsonobject. fromobject (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 "));

Assertions. assertlistequals (expected, (list) propertyutils. getproperty (bean, "array "));

Convert to Bean:
String JSON = "{bool: True, integer: 1, string: \" JSON \"}";

Jsonobject = jsonobject. fromobject (JSON );

Beana bean = (beana) jsonobject. tobean (jsonobject, beana. Class );

Assertequals (jsonobject. Get ("bool"), Boolean. valueof (bean. isbool ()));

Assertequals (jsonobject. Get ("integer"), new INTEGER (bean. getinteger ()));

Assertequals (jsonobject. Get ("string"), Bean. getstring ());

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.