我一直在查指南,但不知道在这里该怎么做. 我有一个很大的TXT文件,需要将其转换为JSON.

但是,根据一行的前3个字符,该行的其余部分适用不同的规则.

case 示例:

001010100092019051513040000000000000000201905150000Skovharevej         Skovharevej                             
001010100102019051513040000000000000000201905150000Svalehalevej        Svalehalevej  
00401010013001 001 U2018092921102150Nordhavn            
00901010013001 001 U20220114101618Hans Egedes                   
01301010013001 001 U2022011410167046Hans Egedes,Kbhvn   
01401010013001 001 U202201141016561. Øst  

如果文本行以001开头,则类型为道路

  1. 第一个3是类型代码
  2. 接下来的4个是通信代码
  3. 下一个4是道路代码
  4. 接下来的12个是时间戳
  5. 下一个4是通往下一个通信代码的道路
  6. 下一个4是通往下一个道路代码的道路
  7. 接下来的4个是前一个社区代码中的道路
  8. 下一个4是先前道路代码中的道路
  9. 下一个12点是开始日期
  10. 接下来的20个是路名
  11. 下一个40是扩展的道路名称

现在,如果类型为004

  1. 然后前3个是类型
  2. 接下来的4个是通信代码
  3. 下一个4是道路代码
  4. 下一个4是门牌号
  5. 下一个4是门牌号
  6. 下一个1是房号是偶数还是偶数
  7. 接下来的12个是时间戳
  8. 下一个4是邮政编码
  9. 接下来20个是邮政区

因此,根据上述信息重申.

  • 我需要在C#中输入一个txt文件

  • 然后读每一行

      var lines = File.ReadLines(fileName); 
      foreach (var line in lines)
    
  • 然后我必须阅读前3个字符,并根据这些确定它是什么类型.

  • 然后,我必须通过一个特定的规则拆分来处理这条线

  • 并最终输出JSON文件

下面的解决方案

控制器

public class Convert控制器
{
    public void convertTxtFile()
    {
        var filename = "C:\\A370715.txt";
        var lines = File.ReadLines(filename);
        string jsonString = "";
        if (!File.Exists("C:\\path.json"))
        {
        TextWriter tsw = new StreamWriter(@"C:\path.json", true);
        foreach (var line in lines)
        {
            System.Console.WriteLine(line);
            var code = line.Substring(0, 3);
            System.Console.WriteLine($"Code: {code}");
            switch (code)
            {
                case "001":
                    班级001 c001 = new 班级001(line);
                    jsonString = JsonSerializer.Serialize(c001);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "002":
                    班级002 c002 = new 班级002(line);
                    jsonString = JsonSerializer.Serialize(c002);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "003":
                    班级003 c003 = new 班级003(line);
                    jsonString = JsonSerializer.Serialize(c003);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "004":
                    班级004 c004 = new 班级004(line);
                    jsonString = JsonSerializer.Serialize(c004);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "005":
                    班级005 c005 = new 班级005(line);
                    jsonString = JsonSerializer.Serialize(c005);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "006":
                    班级006 c006 = new 班级006(line);
                    jsonString = JsonSerializer.Serialize(c006);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "007":
                    班级007 c007 = new 班级007(line);
                    jsonString = JsonSerializer.Serialize(c007);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "008":
                    班级008 c008 = new 班级008(line);
                    jsonString = JsonSerializer.Serialize(c008);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "009":
                    班级009 c009 = new 班级009(line);
                    jsonString = JsonSerializer.Serialize(c009);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "010":
                    班级010 c010 = new 班级010(line);
                    jsonString = JsonSerializer.Serialize(c010);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "011":
                    班级011 c011 = new 班级011(line);
                    jsonString = JsonSerializer.Serialize(c011);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "012":
                    班级012 c012 = new 班级012(line);
                    jsonString = JsonSerializer.Serialize(c012);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "013":
                    班级013 c013 = new 班级013(line);
                    jsonString = JsonSerializer.Serialize(c013);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "014":
                    班级014 c014 = new 班级014(line);
                    jsonString = JsonSerializer.Serialize(c014);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "015":
                    班级015 c015 = new 班级015(line);
                    jsonString = JsonSerializer.Serialize(c015);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "016":
                    班级016 c016 = new 班级016(line);
                    jsonString = JsonSerializer.Serialize(c016);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                default:
                    System.Console.WriteLine($"What to do with code: {code} ?");
                    break;
            }
        }
        System.Console.WriteLine("Convert is finished");
        } else { System.Console.WriteLine("File already exist please remove file before creating a new one!"); }
    }  
}

班级

public class 班级001
{
    public string type { get; set; }
    public string kommuneCode { get; set; }
    public string roadCode { get; set; }
    public string timestamp { get; set; }
    public string roadToNextKommuneCode { get; set; }
    public string roadToNextRoadCode { get; set; }
    public string roadToPreviousKommuneCode { get; set; }
    public string roadToPreviousRoadCode { get; set; }
    public string startDate { get; set; }
    public string roadName { get; set; }
    public string ExpandedRoadName { get; set; }


    public 班级001(string s)
    {
        this.type = s.Substring(0, 3);
        this.kommuneCode = s.Substring(3, 4);
        this.roadCode = s.Substring(7, 4);
        this.timestamp = s.Substring(11, 12);
        this.roadToNextKommuneCode = s.Substring(23, 4);
        this.roadToNextRoadCode = s.Substring(27, 4);
        this.roadToPreviousKommuneCode = s.Substring(31, 4);
        this.roadToPreviousRoadCode = s.Substring(35, 4);
        this.startDate = s.Substring(39, 12);
        this.roadName = s.Substring(51, 20);
        this.ExpandedRoadName = s.Substring(71, 40);
    }
}

推荐答案

您的程序可能如下所示:

 static void Main(string[] args)
    {
        string input = @"001010100092019051513040000000000000000201905150000Skovharevej         Skovharevej                             
001010100102019051513040000000000000000201905150000Svalehalevej        Svalehalevej  
00401010013001 001 U2018092921102150Nordhavn            
00901010013001 001 U20220114101618Hans Egedes                   
01301010013001 001 U2022011410167046Hans Egedes,Kbhvn   
01401010013001 001 U202201141016561. Øst  ";
        foreach (var line in input.Split("\r\n"))
        {
            System.Console.WriteLine(line);
            string code = line.Substring(0, 3);
            System.Console.WriteLine($"Code: {code}");
            switch (code)
            {
                case "001":
                    Class001 c001 = new Class001(line);
                    string jsonString = JsonSerializer.Serialize(c001);
                    System.Console.WriteLine(jsonString);
                    break;
                case "003":
                    //TODO: Handling of class "003"
                    break;
                default:
                    System.Console.WriteLine($"What to do with code: {code} ?");
                    break;
            }
        }

    }

使用Class001:

public class Class001
{
    public string code { get; set; }
    public string kommune { get; set; }
    public string road { get; set; }
    public string timestamp { get; set; }

    /// etc, etc

    public Class001(string s)
    {
        this.code = s.Substring(0, 3);
        this.kommune = s.Substring(3, 4);
        this.road = s.Substring(7, 4);
        this.timestamp = s.Substring(11, 12);
    }
}

此代码应以以下输出开始:

001010100092019051513040000000000000000201905150000Skovharevej         Skovharevej
Code: 001
{"code":"001","kommune":"0101","road":"0009","timestamp":"201905151304"}
001010100102019051513040000000000000000201905150000Svalehalevej        Svalehalevej
Code: 001
{"code":"001","kommune":"0101","road":"0010","timestamp":"201905151304"}
  • 要使JsonSerializer.Serialize()正常工作,您需要添加:using System.Text.Json;

编辑:要创建一个完整的JSON,您可以将此代码更改为:

static void Main(string[] args)
    {
        string input = @"00101010009201905..............";
        List<Object> completeJSON = new List<Object>();
        foreach (var line in input.Split("\r\n"))
        {
            System.Console.WriteLine(line);
            string code = line.Substring(0, 3);
            System.Console.WriteLine($"Code: {code}");
            switch (code)
            {
                case "001":
                    Class001 tmp_c001 = new Class001(line);
                    //string jsonString = JsonSerializer.Serialize(c001);
                    completeJSON.Add(tmp_c001);
                    break;
                case "004":
                    Class004 tmp_c004 = new Class004(line);
                    completeJSON.Add(tmp_c004);
                    break;
                default:
                    System.Console.WriteLine($"What to do with code: {code} ?");
                    break;
            }
        }
      System.Console.WriteLine(JsonSerializer.Serialize(completeJSON));
    }

这应该会带来一些结果(当只考虑输入的前两行时):

[{"code":"001","kommune":"0101","road":"0009","timestamp":"201905151304"},{"code":"001","kommune":"0101","road":"0010","timestamp":"201905151304"}]

Csharp相关问答推荐

为什么C#Bigbit不总是相同的比特长度?

使用LayoutKind在C#中嵌套 struct .显式

如何在C#中将对象[*,*]直接转换为字符串[*,*]?

方法从数据表中只 Select 一个条件?

如何将MongoDB序列化程序设置为内部对象属性

try 在.Net核心身份注册页面中使用AJAX,但没有成功..NET Core 5.0 Razor页面应用程序

确定System.Text.Json序列化中是否无法识别Type

为什么我可以用硬编码的集合而不是变量来设置没有setter的IList属性的值?

如何在CSharp中将json字符串转换为DataTable?

在Windows Plesk上发布ASP.NET Core 7 Web API-错误:无法加载文件或程序集';Microsoft.Data.SqlClient';

Blazor Server.NET 8中的Blazore.FluentValidation问题

源代码生成器:CS8795分部方法';Class1.GetS2(字符串)';必须有实现部分,因为它有可访问性修饰符?

如何实现有条件的自定义Json转换器隐藏属性

C# Winforms:从对象树到TreeView的递归转换重复条目

如何使用实体框架核心对字符串_agg使用强制转换为varchar(Max)

用MongoDB c#驱动程序删除和返回嵌套数组中的文档

如何在特定时间间隔运行多个后台任务?

ASP.NET Core 8 Web API:如何添加版本控制?

PowerShell:如何[引用]数组中的元素?

Firefox中的MudBlazor DataGrid单元格溢出