Пример #1
0
        /// <summary>
        /// DataRow转换为Json格式字符串.
        /// </summary>
        /// <param name="row">数据行.</param>
        /// <param name="filter">是否过滤空数据.</param>
        /// <param name="fields">只是转换的字符串.</param>
        /// <param name="unicode">是否进行Unicode编码.</param>
        /// <returns>
        /// System.String
        /// </returns>
        public static string RowToJson(DataRow row, bool filter, string[] fields, bool unicode)
        {
            if (row == null)
            {
                return("");
            }

            DataColumnCollection cols = row.Table.Columns;
            string colDelimiter       = "";

            StringBuilder result = new StringBuilder("{");

            bool fldNull = Utils.IsNullOrEmpty(fields);

            for (int i = 0; i < cols.Count; i++)
            {
                if (!fldNull && !fields.Contains <string>(cols[i].ColumnName))
                {
                    continue;
                }

                // 获取当前单元格的值
                object val = row[i];

                // 值为null时直接进行下一个数据处理
                if (val == null)
                {
                    continue;
                }

                if (val is DataTable)
                {
                    result.Append(colDelimiter).Append("\"")
                    .Append(cols[i].ColumnName).Append("\":")
                    .Append(TableToJson((DataTable)val, filter, fields, unicode));

                    colDelimiter = ",";
                }
                else if (val is DataRow)
                {
                    result.Append(colDelimiter).Append("\"")
                    .Append(cols[i].ColumnName).Append("\":")
                    .Append(RowToJson((DataRow)val, filter, fields, unicode));

                    colDelimiter = ",";
                }
                else if (val is IDictionary)
                {
                    IDictionary dictionary = val as IDictionary;
                    if (dictionary != null)
                    {
                        result.Append(colDelimiter).Append("\"")
                        .Append(cols[i].ColumnName).Append("\":")
                        .Append(JsonUtils.ConvertJson(dictionary, unicode));

                        colDelimiter = ",";
                    }
                }
                else
                {
                    // use index rather than foreach, so we can use the index for both the row and cols collection
                    string vals = Utils.JsonValueOfType(row[i], cols[i].DataType, unicode);
                    if (filter && vals == "null")
                    {
                        continue;
                    }
                    else
                    {
                        result.Append(colDelimiter).Append("\"")
                        .Append(cols[i].ColumnName).Append("\":")
                        .Append(vals);

                        colDelimiter = ",";
                    }
                }
            }
            result.Append("}");
            return(result.ToString());
        }