1、DataTable转json
public class DataTableConvertJson { #region dataTable转换成Json格式 /// <summary> /// dataTable转换成Json格式 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string DataTable2Json(DataTable dt) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{""); jsonBuilder.Append(dt.TableName); jsonBuilder.Append("":["); jsonBuilder.Append("["); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { jsonBuilder.Append("""); jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append("":""); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append("","); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("},"); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("]"); jsonBuilder.Append("}"); return jsonBuilder.ToString(); } #endregion dataTable转换成Json格式 #region DataSet转换成Json格式 /// <summary> /// DataSet转换成Json格式 /// </summary> /// <param name="ds">DataSet</param> /// <returns></returns> public static string Dataset2Json(DataSet ds) { StringBuilder json = new StringBuilder(); foreach (DataTable dt in ds.Tables) { json.Append("{""); json.Append(dt.TableName); json.Append("":"); json.Append(DataTable2Json(dt)); json.Append("}"); } return json.ToString(); } #endregion /// <summary> /// Msdn /// </summary> /// <param name="jsonName"></param> /// <param name="dt"></param> /// <returns></returns> public static string DataTableToJson(string jsonName, DataTable dt) { StringBuilder Json = new StringBuilder(); 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++) { Json.Append(""" + dt.Columns[j].ColumnName.ToString() + "":"" + dt.Rows[i][j].ToString() + """); if (j < dt.Columns.Count - 1) { Json.Append(","); } } Json.Append("}"); if (i < dt.Rows.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return Json.ToString(); } }
调用 json= DataTableConvertJson.DataTableToJson("tble", tblDatas);
//Quotation_No 为tblDatas的一列
alert(msg.tble[i].Quotation_No)
2、对象转json
System.Runtime.Serialization.Json,杯具的孩子会发现,在添加引用里根本找不到此命名空间,找到的是System.Runtime.Serialization,添加引用后,根本不管用,此命名空间里根本不包括序列化要用到的对象DataContractJsonSerializer,那,这个对象到底在哪里呢。。。。经过不断的查终于找到了,这个坑爹的货藏的很深呀。正确的是直接添加这个引用 System.Servicemodel.Web就把问题解决了。
// using System.Runtime.Serialization.Json; <summary> 解析JSON,仿Javascript风格 </summary> public static class JSON { public static T parse<T>(string jsonString) { using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) { return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms); } } public static string stringify(object jsonObject) { using (var ms = new MemoryStream()) { new DataContractJsonSerializer(jsonObject.GetType()).WriteObject(ms, jsonObject); return Encoding.UTF8.GetString(ms.ToArray()); } }
利用json.net
//Model转JSON Model m = new Model(); m.city = "beijing"; m.city_en = "北京"; string json = JsonConvert.SerializeObject(m); //JSON转Model
m= (Model)JsonConvert.DeserializeObject<Model>(str);