Exemplo n.º 1
0
        /// <summary>
        /// 转置table序列化Json
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public 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();
                    dataItem.Value = row[j].ToString().Trim();
                    columnsJson.Add(i, dataItem);
                    i++;
                }

                resultObject.Add(columnsJson);
            }

            string result = DictionarysToJson(resultObject);

            return result;
        }
Exemplo n.º 2
0
 /// <summary>
 /// 将JsonData转换为json串
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 private static string TranslateJsonData(JsonData data)
 {
     string result = String.Format("\"{0}\":\"{1}\"", data.Name, data.Value);
     return result;
 }