C# 当TSV文件保存为.txt时,如何将其呈现为JSON?
我有一个非常大的表,保存为文本文件,从外部源导入到我的公司。此文件中的格式看起来应该是.TSV。我需要找到一种方法把它放到一个可观察的集合中。我的想法是,我可以使用反序列化程序来自动解析它。这根本不起作用,因为我没有在整个文档中保存任何引号。然后我发现。giotskhada的答案似乎是可行的,但它是Python的,我没有一个中间文件来保存它。以下是txt文件的示例:C# 当TSV文件保存为.txt时,如何将其呈现为JSON?,c#,json,csv,oop,C#,Json,Csv,Oop,我有一个非常大的表,保存为文本文件,从外部源导入到我的公司。此文件中的格式看起来应该是.TSV。我需要找到一种方法把它放到一个可观察的集合中。我的想法是,我可以使用反序列化程序来自动解析它。这根本不起作用,因为我没有在整个文档中保存任何引号。然后我发现。giotskhada的答案似乎是可行的,但它是Python的,我没有一个中间文件来保存它。以下是txt文件的示例: id FieldName1 FieldName2 FieldName3 FieldName4 1
id FieldName1 FieldName2 FieldName3 FieldName4
1 test1 test3 test4
2 test2 test3 test4
3 test1 test2 test3 test4
我希望它能这样读出来:
[ {"id":"1",
"FieldName1":"test1",
"FieldName2":"null",
"FieldName3":"test3",
"FieldName4":"test4"}
},
{"id":"2",
"FieldName1":"null",
"FieldName2":"test2",
"FieldName3":"test3",
"FieldName4":"test4"}
},
{"id":"3",
"FieldName1":"test1",
"FieldName2":"test2",
"FieldName3":"test3",
"FieldName4":"test4"}
]
如何在C#中实现这一点?如果您打开使用外部库,将有助于以预期格式处理大型文件 安装包ChoETL.JSON 下面是示例工作代码
string tsv = @"id FieldName1 FieldName2 FieldName3 FieldName4
1 test1 test3 test4
2 test2 test3 test4
3 test1 test2 test3 test4";
StringBuilder json = new StringBuilder();
using (var r = ChoTSVReader.LoadText(tsv)
.WithFirstLineHeader()
)
{
using (var w = new ChoJSONWriter(json))
w.Write(r);
}
Console.WriteLine(json.ToString());
输出:
[
{
"id": "1",
"FieldName1": "test1",
"FieldName2": null,
"FieldName3": "test3",
"FieldName4": "test4"
},
{
"id": "2",
"FieldName1": null,
"FieldName2": "test2",
"FieldName3": "test3",
"FieldName4": "test4"
},
{
"id": "3",
"FieldName1": "test1",
"FieldName2": "test2",
"FieldName3": "test3",
"FieldName4": "test4"
}
]
希望有帮助。首先,您想要的输出无效。(你可以检查一下。)你真的想要那个特定的输出,还是想要JSON?是的,有点。我希望能够将其读入一个集合,这样我就可以重新创建缓慢变化的维度逻辑,对对象进行排序,并将它们与数据库中已经存在的条目进行比较。我已经编辑了这个问题以显示JSON。谢谢。我会试一试,让你知道!这非常有效。。。非常感谢。现在我要把它放进收藏