public string ConvertJSONtoCSV(string json) { try { DataTable dt = JsonConvert.DeserializeObject <DataTable>(json); StringBuilder sb = new StringBuilder(); IEnumerable <string> columnNames = dt.Columns.Cast <DataColumn>(). Select(column => column.ColumnName); sb.AppendLine(string.Join(ColumnSeparator.ToString(), columnNames)); foreach (DataRow row in dt.Rows) { IEnumerable <string> fields = row.ItemArray.Select(field => { if (field.GetType().IsArray) { return(string.Join(ValueSeparator.ToString(), field as string[])); } return(field.ToString()); }); sb.AppendLine(string.Join(ColumnSeparator.ToString(), fields)); } return(sb.ToString().Trim()); } catch (Exception e) { return("Error! Invalid Json."); } }
/// <summary> /// Produces the data to be written for a single row. /// </summary> /// <param name="cells"></param> /// <returns></returns> public string CreateRow(IEnumerable <string> cells) { string row = string.Join(ColumnSeparator.ToString(), cells.Select(cell => ConvertCell(cell))); return(row); }
protected override void InternalOnInputCompleted() { base.InternalOnInputCompleted(); StringBuilder sb = new StringBuilder(); sb.AppendLine(""); string rowSeparator = ColumnSeparator.ToString(); int nbreColWidth = _buffer.Count.ToString().Length; Func <string, int, string> pad = Alignment switch { TLogRowAlignment.Left => padright, TLogRowAlignment.Right => padleft, _ => padboth }; Func <string[], string> row = Mode switch { TLogRowMode.Basic => (s => string.Join(RowSeparator, s)), _ => (s => { return(RowSeparator + string.Join(RowSeparator, s) + RowSeparator + "\n" + rowSeparator); }), }; if (ShowItemNumber) { rowSeparator += new string(LineSeparator, nbreColWidth + (AdditionalSpace > 0 ? AdditionalSpace : 0)) + ColumnSeparator; } foreach (var(width, index) in _widthes.WithIndex()) { rowSeparator += new string(LineSeparator, width + (AdditionalSpace > 0 ? AdditionalSpace : 0)) + ColumnSeparator; } if (!ShowHeader && Mode == TLogRowMode.Table) { sb.AppendLine(rowSeparator); } using (_logger.BeginScope("TLogRow")) { foreach (var(items, col) in _buffer.WithIndex()) { if (!ShowHeader && col == 0) { continue; } if (ShowHeader && col == 0 && Mode == TLogRowMode.Table) { sb.AppendLine(rowSeparator); } foreach (var(value, index) in items.WithIndex()) { items[index] = pad(value, _widthes[index] + (AdditionalSpace > 0 ? AdditionalSpace : 0)); } if (ShowItemNumber) { sb.Append(RowSeparator + pad(col == 0 ? "#" : col.ToString(), nbreColWidth + (AdditionalSpace > 0 ? AdditionalSpace : 0))); if (Mode == TLogRowMode.Basic) { sb.Append(RowSeparator); } } sb.AppendLine(row(items)); } _logger.LogInformation(sb.ToString()); } }