Download JAVA & amp; JSON from the java Official Website

Source: Internet
Author: User

Download JAVA & JSON from the java Official Website


Public static String object2json (Object obj ){
StringBuilder json = new StringBuilder ();
If (obj = null ){
Json. append ("\"\"");
} Else if (obj instanceof String | obj instanceof Integer
| Obj instanceof Float | obj instanceof Boolean
| Obj instanceof Short | obj instanceof Double
| Obj instanceof Long | obj instanceof BigDecimal
| Obj instanceof BigInteger | obj instanceof Byte ){
Json. append ("\" "). append (string2json (obj. toString (). append ("\"");
} Else if (obj instanceof Object []) {
Json. append (array2json (Object []) obj ));
} Else if (obj instanceof List ){
Json. append (list2json (List <?>) Obj ));
} Else if (obj instanceof Map ){
Json. append (map2json (Map <?, ?>) Obj ));
} Else if (obj instanceof Set ){
Json. append (set2json (Set <?>) Obj ));
} Else {
Json. append (bean2json (obj ));
}
Return json. toString ();
}

Public static String bean2json (Object bean ){
StringBuilder json = new StringBuilder ();
Json. append ("{");
PropertyDescriptor [] props = null;
Try {
Props = Introspector. getBeanInfo (bean. getClass (), Object. class)
. GetPropertyDescriptors ();
} Catch (IntrospectionException e ){
}
If (props! = Null ){
For (int I = 0; I <props. length; I ++ ){
Try {
String name = object2json (props [I]. getName ());
String value = object2json (props [I]. getReadMethod (). invoke (
Bean ));
Json. append (name );
Json. append (":");
Json. append (value );
Json. append (",");
} Catch (Exception e ){
Logger. error ("Exception raised -------->", e );
}
}
Json. setCharAt (json. length ()-1 ,'}');
} Else {
Json. append ("}");
}
Return json. toString ();
}

Public static String list2json (List <?> List ){
StringBuilder json = new StringBuilder ();
Json. append ("[");
If (list! = Null & list. size ()> 0 ){
For (Object obj: list ){
Json. append (object2json (obj ));
Json. append (",");
}
Json. setCharAt (json. length ()-1, ']');
} Else {
Json. append ("]");
}
Return json. toString ();
}

Public static String array2json (Object [] array ){
StringBuilder json = new StringBuilder ();
Json. append ("[");
If (array! = Null & array. length> 0 ){
For (Object obj: array ){
Json. append (object2json (obj ));
Json. append (",");
}
Json. setCharAt (json. length ()-1, ']');
} Else {
Json. append ("]");
}
Return json. toString ();
}

Public static String map2json (Map <?, ?> Map ){
StringBuilder json = new StringBuilder ();
Json. append ("{");
If (map! = Null & map. size ()> 0 ){
For (Object key: map. keySet ()){
Json. append (object2json (key ));
Json. append (":");
Json. append (object2json (map. get (key )));
Json. append (",");
}
Json. setCharAt (json. length ()-1 ,'}');
} Else {
Json. append ("}");
}
Return json. toString ();
}

Public static String set2json (Set <?> Set ){
StringBuilder json = new StringBuilder ();
Json. append ("[");
If (set! = Null & set. size ()> 0 ){
For (Object obj: set ){
Json. append (object2json (obj ));
Json. append (",");
}
Json. setCharAt (json. length ()-1, ']');
} Else {
Json. append ("]");
}
Return json. toString ();
}

Public static String string2json (String s ){
If (s = null)
Return "";
StringBuilder sb = new StringBuilder ();
For (int I = 0; I <s. length (); I ++ ){
Char ch = s. charAt (I );
Switch (ch ){
Case '"':
Sb. append ("\\\"");
Break;
Case '\\':
Sb. append ("\\\\");
Break;
Case '\ B ':
Sb. append ("\ B ");
Break;
Case '\ F ':
Sb. append ("\ f ");
Break;
Case '\ N ':
Sb. append ("\ n ");
Break;
Case '\ R ':
Sb. append ("\ r ");
Break;
Case '\ t ':
Sb. append ("\ t ");
Break;
Case '/':
Sb. append ("\\/");
Break;
Default:
If (ch> = '\ u000000' & ch <=' \ u001f '){
String ss = Integer. toHexString (ch );
Sb. append ("\ u ");
For (int k = 0; k <4-ss. length (); k ++ ){
Sb. append ('0 ');
}
Sb. append (ss. toUpperCase ());
} Else {
Sb. append (ch );
}
}
}
Return sb. toString ();
}

/*************************************** ************************************
* Serialize an object to JSON text
*
* @ Param object
* @ Return
*/
Public static String toJSONString (Object object ){
JSONArray jsonArray = JSONArray. fromObject (object );

Return jsonArray. toString ();
}

/*************************************** ************************************
* Serialize the JSON object array to JSON text
*
* @ Param jsonArray
* @ Return
*/
Public static String toJSONString (JSONArray jsonArray ){
Return jsonArray. toString ();
}

/*************************************** ************************************
* Serialize a json object 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 JSON object Array
*
* @ Param object
* @ Return
*/
Public static JSONArray toJSONArray (Object 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 = 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;
}

Public static TreeMap toTreeMap (Object object ){
TreeMap <String, Object> data = new TreeMap <String, Object> ();
JSONObject jsonObject = 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;
}

Public static LinkedHashMap toLinkedHashMap (Object object Object ){
LinkedHashMap <String, Object> data = new LinkedHashMap <String, Object> ();
JSONObject jsonObject = 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 the List of non-Object 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;
}

/*************************************** ************************************
* Convert the array of JSON objects to a List of input 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 input types.
*
* @ Param <T>
* @ Param
* @ 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 input object.
*
* @ Param <T>
* @ Param jsonObject
* @ Param beanClass
* @ Return
*/
Public static <T> T toBean (JSONObject jsonObject, Class <T> beanClass ){
Return (T) JSONObject. toBean (jsonObject, beanClass );
}

/*************************************** ************************************
* Convert an object to an input object.
*
* @ 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 );
}

/*************************************** ************************************
* Deserializes JSON text into the entity of the slave Link
*
* @ Param <T>
* Generic T represents the primary entity type
* @ Param <D>
* Generic D indicates the slave object type.
* @ Param jsonString
* JSON text
* @ Param mainClass
* Primary object type
* @ Param detailName
* Attribute names of the object classes in the primary object classes
* @ Param detailClass
* From Object 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 = toBean (jsonObject, mainClass );
List <D> detailList = toList (jsonArray, detailClass );

Try {
BeanUtils. setProperty (mainEntity, detailName, detailList );
} Catch (Exception ex ){
Throw new RuntimeException ("An error occurred while deserializing the object from the master-slave relationship JSON! ");
}

Return mainEntity;
}

/*************************************** ************************************
* Deserializes JSON text into the entity of the slave Link
*
* @ Param <T> generic T
* Represents the primary object type
* @ Param <D1> generic D1
* Indicates the type of the slave object.
* @ Param <D2> generic D2
* Indicates the type of the slave object.
* @ Param jsonString
* JSON text
* @ Param mainClass
* Primary object type
* @ Param detailName1
* Attributes of an object class in the primary object class
* @ Param detailClass1
* From Object Type
* @ Param detailName2
* Attributes of an object class in the primary object class
* @ Param detailClass2
* From Object 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 = toBean (jsonObject, mainClass );
List <D1> detailList1 = toList (jsonArray1, detailClass1 );
List <D2> detailList2 = toList (jsonArray2, detailClass2 );

Try {
BeanUtils. setProperty (mainEntity, detailName1, detailList1 );
BeanUtils. setProperty (mainEntity, detailName2, detailList2 );
} Catch (Exception ex ){
Throw new RuntimeException ("An error occurred while deserializing the object from the master-slave relationship JSON! ");
}

Return mainEntity;
}

/*************************************** ************************************
* Deserializes JSON text into the entity of the slave Link
*
* @ Param <T> generic T
* Represents the primary object type
* @ Param <D1> generic D1
* Indicates the type of the slave object.
* @ Param <D2> generic D2
* Indicates the type of the slave object.
* @ Param jsonString
* JSON text
* @ Param mainClass
* Primary object type
* @ Param detailName1
* Attributes of an object class in the primary object class
* @ Param detailClass1
* From Object Type
* @ Param detailName2
* Attributes of an object class in the primary object class
* @ Param detailClass2
* From Object Type
* @ Param detailName3
* Attributes of an object class in the primary object class
* @ Param detailClass3
* From Object 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 = toBean (jsonObject, mainClass );
List <D1> detailList1 = toList (jsonArray1, detailClass1 );
List <D2> detailList2 = toList (jsonArray2, detailClass2 );
List <D3> detailList3 = 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 ("An error occurred while deserializing the object from the master-slave relationship JSON! ");
}

Return mainEntity;
}

/*************************************** ************************************
* Deserializes JSON text into the entity of the slave Link
*
* @ Param <T>
* Primary object type
* @ Param jsonString
* JSON text
* @ Param mainClass
* Primary object type
* @ Param detailClass
* Multiple slave entities are stored in the primary object. The attribute names and types
* @ Return
*/
Public static <T> T toBean (String jsonString, Class <T> mainClass,
HashMap <String, Class> detailClass ){
JSONObject jsonObject = JSONObject. fromObject (jsonString );
T mainEntity = 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 ("An error occurred while deserializing the object from the master-slave relationship JSON! ");
}
}
Return mainEntity;
}

Public static String toXML (String reportName, List list ){
// A new StringBuilder is used to receive the XML value to be passed.
StringBuilder strBder = new StringBuilder ();
PrintWriter writer = null;

String resultName = null;
String resultCount = null;

StrBder. append ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ");
StrBder. append ("<graph caption = \" "+ reportName +" \ "" +
"ShowNames = \" 1 \ "decimalPrecision = \" 1 \ "numberFormatScale = \" 1 \ "baseFontSize = \" 14 \ "" +
"FormatNumber = \" 1 \ "showValues = \" 0 \ "> ");

For (int I = 0; I <list. size (); I ++ ){
Map currMap = (Map) list. get (I );
ResultName = currMap. get ("resultName"). toString ();

ResultCount = currMap. get ("resultCount"). toString ();

StrBder. append ("<set name = \" "+ resultName +" \ "value = \" "+ resultCount +" \ "/> ");

}

StrBder. append ("</graph> ");
Return strBder. toString ();
}

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.