/// <summary> /// 转置table序列化Json /// </summary> /// <param name="dataTable"></param> /// <returns></returns> private static string TransformTableToJson(DataTable dataTable) { IList<Dictionary<int, JsonData>> resultObject = new List<Dictionary<int, JsonData>>(); for (int j = 0; j < dataTable.Columns.Count; j++) { int i = 0; Dictionary<int, JsonData> columnsJson = new Dictionary<int, JsonData>(); JsonData header = new JsonData(); header.Name = "RowId"; header.Value = dataTable.Columns[j].ColumnName; columnsJson.Add(i, header); i++; foreach (DataRow row in dataTable.Rows) { JsonData dataItem = new JsonData(); dataItem.Name = "Col" + i.ToString(); string m_ColumnValue = GetConfigInfo.FormatDecimalPlaces(row[j], dataTable.Columns[j].DataType); //增加保留小数点功能 dataItem.Value = m_ColumnValue.Trim(); //dataItem.Value = row[j].ToString().Trim(); columnsJson.Add(i, dataItem); i++; } resultObject.Add(columnsJson); } string result = DictionarysToJson(resultObject); return result; }
private static string TranslateJsonData(JsonData data) { string result = String.Format("\"{0}\":\"{1}\"", data.Name, data.Value); return result; }