JSON conversion class (2) -- convert List to Json, convert object set to Json, etc. -- listjson

Source: Internet
Author: User
Tags tojson

JSON conversion class (2) -- convert List to Json, convert object set to Json, etc. -- listjson

# Region List to Json // <summary> // List to Json // </summary> public static string ListToJson <T> (IList <T> list) {object obj = list [0]; return ListToJson <T> (list, obj. getType (). name) ;}/// <summary> /// List to Json /// </summary> public static string ListToJson <T> (IList <T> list, string jsonName) {StringBuilder Json = new StringBuilder (); if (string. isNullOrEmpty (jsonName) jsonName = list [0]. getType (). name; Json. append ("{\" "+ jsonName +" \ ": ["); if (list. count> 0) {for (int I = 0; I <list. count; I ++) {T obj = Activator. createInstance <T> (); PropertyInfo [] pi = obj. getType (). getProperties (); Json. append ("{"); for (int j = 0; j <pi. length; j ++) {Type type = pi [j]. getValue (list [I], null ). getType (); Json. append ("\" "+ pi [j]. name. toString () + "\": "+ StringFormat (pi [j]. getValue (list [I], null ). toString (), type); if (j <pi. length-1) {Json. append (",") ;}} Json. append ("}"); if (I <list. count-1) {Json. append (",") ;}} Json. append ("]}"); return Json. toString ();} # endregion # convert the region object to Json // <summary> // convert the object to Json /// </summary> /// <param name = "jsonObject"> Object </param> // <returns> Json string </returns> public static string ToJson (object jsonObject) {string jsonString = "{"; PropertyInfo [] propertyInfo = jsonObject. getType (). getProperties (); for (int I = 0; I <propertyInfo. length; I ++) {object objectValue = propertyInfo [I]. getGetMethod (). invoke (jsonObject, null); string value = string. empty; if (objectValue is DateTime | objectValue is Guid | objectValue is TimeSpan) {value = "'" + objectValue. toString () + "'";} else if (objectValue is string) {value = "'" + ToJson (objectValue. toString () + "'";} else if (objectValue is IEnumerable) {value = ToJson (IEnumerable) objectValue);} else {value = ToJson (objectValue. toString ();} jsonString + = "\" "+ ToJson (propertyInfo [I]. name) + "\": "+ value +", ";} jsonString. remove (jsonString. length-1, jsonString. length); return jsonString + "}";} # endregion # convert the region object set to Json // <summary> // convert the object set to Json /// </summary> /// <param name = "array"> set object </param> /// <returns> Json string </returns> public static string ToJson (IEnumerable array) {string jsonString = "["; foreach (object item in array) {jsonString + = ToJson (item) + "," ;} jsonString. remove (jsonString. length-1, jsonString. length); return jsonString + "]";} # endregion # region normal set conversion Json // <summary> // normal set conversion Json /// </summary> /// <param name = "array"> set object </param> /// <returns> Json string </returns> public static string ToArrayString (IEnumerable array) {string jsonString = "["; foreach (object item in array) {jsonString = ToJson (item. toString () + ",";} jsonString. remove (jsonString. length-1, jsonString. length); return jsonString + "]";} # endregion # convert region DataSet to Json /// <summary> // convert DataSet to Json /// </summary> /// <param name = "dataSet"> DataSet object </param> /// <returns> Json string </returns> public static string ToJson (DataSet dataSet) {string jsonString = "{"; foreach (DataTable table in dataSet. tables) {jsonString + = "\" "+ table. tableName + "\": "+ ToJson (table) +", ";} jsonString = jsonString. trimEnd (','); return jsonString + "}";} # endregion # region Datatable to Json // <summary> // Datatable to Json // </summary> // <param name = "table"> Datatable object </param> /// <returns> Json string </returns> public static string ToJson (DataTable dt) {StringBuilder jsonString = new StringBuilder (); jsonString. append ("["); DataRowCollection drc = dt. rows; for (int I = 0; I <drc. count; I ++) {jsonString. append ("{"); for (int j = 0; j <dt. columns. count; j ++) {string strKey = dt. columns [j]. columnName; string strValue = drc [I] [j]. toString (); Type type = dt. columns [j]. dataType; jsonString. append ("\" "+ strKey +" \ ":"); strValue = StringFormat (strValue, type); if (j <dt. columns. count-1) {jsonString. append (strValue + ",");} else {jsonString. append (strValue) ;}} jsonString. append ("},");} jsonString. remove (jsonString. length-1, 1); jsonString. append ("]"); return jsonString. toString () ;}/// <summary> // DataTable to be converted to Json /// </summary> public static string ToJson (DataTable dt, string jsonName) {StringBuilder Json = new StringBuilder (); if (string. isNullOrEmpty (jsonName) jsonName = dt. tableName; Json. append ("{\" "+ jsonName +" \ ": ["); if (dt. rows. count> 0) {for (int I = 0; I <dt. rows. count; I ++) {Json. append ("{"); for (int j = 0; j <dt. columns. count; j ++) {Type type = dt. rows [I] [j]. getType (); Json. append ("\" "+ dt. columns [j]. columnName. toString () + "\": "+ StringFormat (dt. rows [I] [j]. toString (), type); if (j <dt. columns. count-1) {Json. append (",") ;}} Json. append ("}"); if (I <dt. rows. count-1) {Json. append (",") ;}} Json. append ("]}"); return Json. toString ();} # endregion # region DataReader to Json // <summary> // DataReader to Json // </summary> /// <param name = "dataReader"> DataReader object </param> /// <returns> Json string </returns> public static string ToJson (DbDataReader dataReader) {StringBuilder jsonString = new StringBuilder (); jsonString. append ("["); while (dataReader. read () {jsonString. append ("{"); for (int I = 0; I <dataReader. fieldCount; I ++) {Type type = dataReader. getFieldType (I); string strKey = dataReader. getName (I); string strValue = dataReader [I]. toString (); jsonString. append ("\" "+ strKey +" \ ":"); strValue = StringFormat (strValue, type); if (I <dataReader. fieldCount-1) {jsonString. append (strValue + ",");} else {jsonString. append (strValue) ;}} jsonString. append ("},");} dataReader. close (); jsonString. remove (jsonString. length-1, 1); jsonString. append ("]"); return jsonString. toString () ;}# endregion

 


How to convert a java object to a json data format containing a collection of objects using the JSON-lib framework

List list = new ArraryList ();
List. add (employee); // add class employee
List. add (employer); // add class employer
JsonArray = JSONArray. fromObject (list); // convert to a json Array
That's all.

Use jquery to convert the object set encapsulated by hibernate to the json type, but sbjk

This should be converted in the background. jquery should not be used. If LZ uses struts2 or webwork, it provides a json jar package, which specifically returns the json type.

Related Article

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.