[英]How to convert list to json in c#
I have converted CSV to a list of string I want to create a JSON using the header of CSV file the input and output are as mentioned below I have converted CSV to a list of string I want to create a JSON using the header of CSV file the input and output are as mentioned below
var list = [
[a,b,c],
[1,2,3],
[4,5,6]
];
to至
var json = {
{
"a":1,
"b":2,
"c":3
},
{
"a":4,
"b":5,
"c":6
}
}
Assuming your list looks like假设您的列表看起来像
var list = new string[3][] {
new string[3] { "a","b","c" },
new string[3] {"1","2","3" },
new string[3] {"4","5","6"}
};
You can make a json from it using this method:您可以使用以下方法从中制作 json:
public static string toJson(string[][] csvArray) {
var sb = new StringBuilder("{\n\"elements\": [\n");
var headers = new string[csvArray[0].Length];
for(int i = 0; i < csvArray[0].Length; i++)
{
headers[i] = csvArray[0][i];
}
for(int i = 1; i < csvArray.Length; i++)
{
sb.AppendLine("{");
for(int j = 0; j < csvArray[i].Length; j++)
{
sb.AppendLine("\"" + headers.ElementAtOrDefault(j) + "\": \"" + csvArray[i][j] + "\",");
}
sb.Remove(sb.Length-3, 3); // remove last ",/n" to make json valid
sb.AppendLine("\n},");
}
sb.Remove(sb.Length-3, 3); // remove last ",/n" to make json valid
sb.AppendLine("\n]\n}");
return sb.ToString();
}
Check it in fiddler .在 fiddler 中检查它。 Some alterations might be needed to adjust to your input data, maybe some additional validations, but it will let you go forward.可能需要进行一些更改以适应您的输入数据,可能需要进行一些额外的验证,但它会让您向前推进 go。
Using CSV Helper you can deserialize to Dynamic, ref doc .使用CSV 助手,您可以反序列化为动态,参考文档。
Then you can serialize the result to json using JSON.Net ref .然后,您可以使用 JSON.Net ref将结果序列化为 json 。
var txtInput = "a,b,c\n1,2,3\n4,5,6";
string output;
using (var reader = new StringReader(txtInput))
using (var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csvReader.GetRecords<dynamic>();
output = JsonConvert.SerializeObject(records, Formatting.Indented);
}
Result:结果:
[
{
"a": "1",
"b": "2",
"c": "3"
},
{
"a": "4",
"b": "5",
"c": "6"
}
]
Live Demo: https://dotnetfiddle.net/lF0zJ2现场演示: https://dotnetfiddle.net/lF0zJ2
Note that for CSV file you can use a StreamReader
like:请注意,对于 CSV 文件,您可以使用StreamReader
,例如:
using (var reader = new StreamReader("path\\to\\file.csv"))
While I don't recommend using dynamic typing.虽然我不建议使用动态类型。 For reading a CSV I will recommend writing the proper class representing the data and the mapping class in order to let CSV Helper do everything: type check, unordered column, missing column, missing header, duplicate name column, different separator, multiline data, quoted field etc etc. For reading a CSV I will recommend writing the proper class representing the data and the mapping class in order to let CSV Helper do everything: type check, unordered column, missing column, missing header, duplicate name column, different separator, multiline data, quoted领域等
Disclaimer:免责声明:
This answers doesn't match the given input and ouput.这个答案与给定的输入和输出不匹配。 I choosed to take into account only the part that was valid.我选择只考虑有效的部分。 If you need the ouput to be int instead of string.如果您需要输出为 int 而不是 string。 You can create a class representing the Csv Object (CSV helper Getting Started ).您可以创建一个 class 代表 Csv Object(CSV 助手入门)。 And use csvReader.GetRecords<MyClass>()
.并使用csvReader.GetRecords<MyClass>()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.