public static CsvLine Load(string srcText, char delimeter = ',') { CsvLine line = new CsvLine(); bool quote = false; char escapeChar = '~'; string value = ""; string[] tempValues; if (escapeChar == delimeter) { if (!srcText.Contains('^')) { escapeChar = '^'; } } if (!String.IsNullOrEmpty(srcText)) { for (int x = 0; x < srcText.Length; x++) { if (srcText[x] == '\u0022') { quote = !quote; } if (quote) { value += srcText[x]; } else { if (srcText[x] == delimeter) { value += escapeChar; } else { if (srcText[x] != ' ') { value += srcText[x]; } } } } } tempValues = value.Split(new char[] { escapeChar }, StringSplitOptions.RemoveEmptyEntries); foreach (string v in tempValues) { line.AddValue(v.Trim()); } return(line); }
public static CSV JsonToCsv(string jsonSrc) { CSV csv = null; if (Json.IsSrcValid(jsonSrc)) { Json json = Json.Load(jsonSrc); List <CsvLine> lines = new List <CsvLine>(); CsvLine fields = new CsvLine(); foreach (JsonKeyValue keyValue in json.Objects.First().KeyValues) { fields.AddValue(keyValue.Key); if (keyValue == json.Objects.First().KeyValues.Last()) { lines.Add(fields); } } foreach (JsonObject obj in json.Objects) { CsvLine line = new CsvLine(); foreach (JsonKeyValue keyValue in obj.KeyValues) { line.AddValue(keyValue.Value.ToString()); } lines.Add(line); } csv = new CSV(); csv.AddLines(lines); } return(csv); }