Java Package Jsonutils Tool class and usage

Source: Internet
Author: User

In Java, the Jsonobject and Jsonarray base classes are provided in the Json-lib-2.3-jdk15.jar package for JSON serialization and deserialization operations. But we are more accustomed to encapsulating it further and achieving better reuse.

The encapsulated JSON tool class Jsonutils.java code is as follows:


Jsonutils Code, click Expand

Import java.util.ArrayList;
Import java.util.Collection;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;
Import Org.apache.commons.beanutils.BeanUtils;

public class Jsonutils
{
/**
*
* @author Wangwei JSON tool class
* @param <T>
*
*/

/***
* Serialize List object to JSON text
*/
public static <T> String tojsonstring (list<t> List)
{
Jsonarray Jsonarray = jsonarray.fromobject (list);

return jsonarray.tostring ();
}

/***
* Serializes objects into JSON text
* @param Object
* @return
*/
public static String toJSONString (Object object)
{
Jsonarray Jsonarray = Jsonarray.fromobject (object);

return jsonarray.tostring ();
}

/***
* Serializes a JSON object array into JSON text
* @param Jsonarray
* @return
*/
public static String tojsonstring (Jsonarray jsonarray)
{
return jsonarray.tostring ();
}

/***
* Serializes JSON objects into JSON text
* @param jsonobject
* @return
*/
public static String tojsonstring (Jsonobject jsonobject)
{
return jsonobject.tostring ();
}

/***
* Convert an object to a list object
* @param Object
* @return
*/
public static List Toarraylist (Object object)
{
List arrayList = new ArrayList ();

Jsonarray Jsonarray = Jsonarray.fromobject (object);

Iterator it = Jsonarray.iterator ();
while (It.hasnext ())
{
Jsonobject Jsonobject = (jsonobject) it.next ();

Iterator keys = Jsonobject.keys ();
while (Keys.hasnext ())
{
Object key = Keys.next ();
Object value = Jsonobject.get (key);
Arraylist.add (value);
}
}

return arrayList;
}

/***
* Convert an object to a collection object
* @param Object
* @return
*/
public static Collection Tocollection (Object object)
{
Jsonarray Jsonarray = Jsonarray.fromobject (object);

Return jsonarray.tocollection (Jsonarray);
}

/***
* Convert an object to an array of JSON objects
* @param Object
* @return
*/
public static Jsonarray Tojsonarray (Object object)
{
return Jsonarray.fromobject (object);
}

/***
* Convert an object to a JSON object
* @param Object
* @return
*/
public static Jsonobject Tojsonobject (Object object)
{
return Jsonobject.fromobject (object);
}

/***
* Convert an object to HashMap
* @param Object
* @return
*/
public static HashMap Tohashmap (Object object)
{
hashmap<string, object> data = new hashmap<string, object> ();
Jsonobject Jsonobject = Jsonutils.tojsonobject (object);
Iterator it = Jsonobject.keys ();
while (It.hasnext ())
{
String key = String.valueof (It.next ());
Object value = Jsonobject.get (key);
Data.put (key, value);
}

return data;
}

/***
* Convert an object to list<map<string,object>>
* @param Object
* @return
*/
Returns a list of non-entity types (map<string,object>)
public static list<map<string, object>> toList (Object object)
{
list<map<string, object>> list = new arraylist<map<string, object>> ();
Jsonarray Jsonarray = Jsonarray.fromobject (object);
for (Object Obj:jsonarray)
{
Jsonobject Jsonobject = (jsonobject) obj;
map<string, object> map = new hashmap<string, object> ();
Iterator it = Jsonobject.keys ();
while (It.hasnext ())
{
String key = (string) it.next ();
Object value = Jsonobject.get (key);
Map.put (String) key, value);
}
List.add (map);
}
return list;
}

/***
* Converts an array of JSON objects into a list of incoming types
* @param <T>
* @param Jsonarray
* @param objectClass
* @return
*/
public static <T> list<t> toList (Jsonarray jsonarray, class<t> objectClass)
{
Return Jsonarray.tolist (Jsonarray, ObjectClass);
}

/***
* Convert an object to a list of incoming types
* @param <T>
* @param Jsonarray
* @param objectClass
* @return
*/
public static <T> list<t> toList (Object object, Class<t> objectClass)
{
Jsonarray Jsonarray = Jsonarray.fromobject (object);

Return Jsonarray.tolist (Jsonarray, ObjectClass);
}

/***
* Convert a JSON object to an object of an incoming type
* @param <T>
* @param jsonobject
* @param beanclass
* @return
*/
public static <T> T Tobean (jsonobject jsonobject, class<t> beanclass)
{
Return (T) Jsonobject.tobean (Jsonobject, Beanclass);
}

/***
* Converts an object to an object of an incoming type
* @param <T>
* @param Object
* @param beanclass
* @return
*/
public static <T> T Tobean (Object object, Class<t> beanclass)
{
Jsonobject Jsonobject = Jsonobject.fromobject (object);

Return (T) Jsonobject.tobean (Jsonobject, Beanclass);
}

/***
* Deserialize JSON text as the entity from the relationship
* @param <T> Generic T represents the primary entity type
* @param <D> generic D represents from entity type
* @param jsonstring JSON text
* @param mainClass Primary entity type
* @param detailname attribute names from entity classes in the primary entity class
* @param detailclass from entity type
* @return
*/
public static <t, d> T Tobean (String jsonstring, class<t> MainClass,
String Detailname, class<d> detailclass)
{
Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
Jsonarray Jsonarray = (jsonarray) jsonobject.get (detailname);

T mainentity = Jsonutils.tobean (Jsonobject, MainClass);
list<d> detaillist = jsonutils.tolist (Jsonarray, Detailclass);

Try
{
Beanutils.setproperty (mainentity, Detailname, detaillist);
}
catch (Exception ex)
{
throw new RuntimeException ("Master-Detail JSON deserialization entity failed!") ");
}

return mainentity;
}

/***
* Deserialize JSON text as the entity from the relationship
* @param <T> Generic T represents the primary entity type
* @param <D1> generics D1 represent the entity type
* @param <D2> generics D2 represent the entity type
* @param jsonstring JSON text
* @param mainClass Primary entity type
* @param detailName1 attributes from the entity class in the primary entity class
* @param detailClass1 from entity type
* @param detailName2 attributes from the entity class in the primary entity class
* @param detailClass2 from entity type
* @return
*/
public static <t, D1, d2> T Tobean (String jsonstring, class<t> MainClass,
String detailName1, Class<d1> detailClass1, String detailName2,
Class<d2> detailClass2)
{
Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
Jsonarray jsonArray1 = (jsonarray) jsonobject.get (detailName1);
Jsonarray jsonArray2 = (jsonarray) jsonobject.get (detailName2);

T mainentity = Jsonutils.tobean (Jsonobject, MainClass);
list<d1> detailList1 = jsonutils.tolist (JsonArray1, DETAILCLASS1);
list<d2> detailList2 = jsonutils.tolist (JsonArray2, DETAILCLASS2);

Try
{
Beanutils.setproperty (mainentity, detailName1, DetailList1);
Beanutils.setproperty (mainentity, detailName2, DetailList2);
}
catch (Exception ex)
{
throw new RuntimeException ("Master-Detail JSON deserialization entity failed!") ");
}

return mainentity;
}

/***
* Deserialize JSON text as the entity from the relationship
* @param <T> Generic T represents the primary entity type
* @param <D1> generics D1 represent the entity type
* @param <D2> generics D2 represent the entity type
* @param jsonstring JSON text
* @param mainClass Primary entity type
* @param detailName1 attributes from the entity class in the primary entity class
* @param detailClass1 from entity type
* @param detailName2 attributes from the entity class in the primary entity class
* @param detailClass2 from entity type
* @param detailName3 attributes from the entity class in the primary entity class
* @param DETAILCLASS3 from entity type
* @return
*/
public static <t, D1, D2, d3> T Tobean (String jsonstring,
Class<t> MainClass, String detailName1, class<d1> DetailClass1,
String detailName2, Class<d2> detailClass2, String DetailName3,
Class<d3> DETAILCLASS3)
{
Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
Jsonarray jsonArray1 = (jsonarray) jsonobject.get (detailName1);
Jsonarray jsonArray2 = (jsonarray) jsonobject.get (detailName2);
Jsonarray jsonArray3 = (jsonarray) jsonobject.get (DETAILNAME3);

T mainentity = Jsonutils.tobean (Jsonobject, MainClass);
list<d1> detailList1 = jsonutils.tolist (JsonArray1, DETAILCLASS1);
list<d2> detailList2 = jsonutils.tolist (JsonArray2, DETAILCLASS2);
list<d3> detailList3 = jsonutils.tolist (JsonArray3, DETAILCLASS3);

Try
{
Beanutils.setproperty (mainentity, detailName1, DetailList1);
Beanutils.setproperty (mainentity, detailName2, DetailList2);
Beanutils.setproperty (mainentity, DetailName3, DETAILLIST3);
}
catch (Exception ex)
{
throw new RuntimeException ("Master-Detail JSON deserialization entity failed!") ");
}

return mainentity;
}

/***
* Deserialize JSON text as the entity from the relationship
* @param <T> Primary entity type
* @param jsonstring JSON text
* @param mainClass Primary entity type
* @param detailclass Store multiple attribute names and types from the entity in the primary entity
* @return
*/
public static <T> T Tobean (String jsonstring, class<t> MainClass,
Hashmap<string, class> Detailclass)
{
Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
T mainentity = Jsonutils.tobean (Jsonobject, MainClass);
For (Object Key:detailClass.keySet ())
{
Try
{
Class value = (Class) Detailclass.get (key);
Beanutils.setproperty (Mainentity, key.tostring (), value);
}
catch (Exception ex)
{
throw new RuntimeException ("Master-Detail JSON deserialization entity failed!") ");
}
}
return mainentity;
}
}

The jar packages required in the above code are: Json-lib-2.3-jdk15.jar,commons-beanutils-1.8.0.jar

The specific usage will be described in encapsulating the JSON tool class in Java and using (b).


By:http://hi.baidu.com/kfo2046/blog/item/a8ce28387b6865ffb211c7af.html

Java Package Jsonutils Tool class and usage

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.