代码之家  ›  专栏  ›  技术社区  ›  Jishan

将多个json文件合并为单个文件

  •  0
  • Jishan  · 技术社区  · 8 年前

    我正在读取多个json文件:

    using (var fbd = new FolderBrowserDialog())
    {
        DialogResult result = fbd.ShowDialog();
    
        if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
        {
            IEnumerable<string> allJSONFiles = GetFileList("*.json", fbd.SelectedPath);
            txtOutput.Clear();
            txtOutput.Text = "Number of files found: " + allJSONFiles.ToList().Count + "\n";
            foreach (string filename in allJSONFiles)
            {
                txtOutput.Text += filename + "\n";
            }                   
        }
    }
    

    现在,json文件大致如下所示,包含多个对象:

    [{
            "Domain": "example.com",
            "A": ["50.63.202.28"],
            "MX": ["0 example-com.mail.protection.outlook.com."],
            "NS": ["ns48.example.com.", "ns47.example.com."],
            "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
            "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -all\""],
            "Country": ["United States"],
            "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
            "SSL": ["None"],
            "WHOIS": [1096],
            "TTL": ["568"]
        }, {
            "Domain": "example.org",
            "A": ["50.63.202.59"],
            "MX": ["30 ALT2.ASPMX.L.GOOGLE.COM.", "20 ALT1.ASPMX.L.GOOGLE.COM.", "50 ASPMX3.GOOGLEMAIL.COM.", "10 ASPMX.L.GOOGLE.COM.", "40 ASPMX2.GOOGLEMAIL.COM."],
            "NS": ["ns13.example.com.", "ns14.example.com."],
            "SOA": ["ns13.example.com. dns.jomax.net. 2016081700 28800 7200 604800 600"],
            "Country": ["United States"],
            "Hostname": ["'ip-50-63-202-59.ip.secureserver.net'"],
            "SSL": ["None"],
            "WHOIS": [5844],
            "TTL": ["569"]
        }
    ]
    

    我要做的就是将这些对象从我正在读取的多个文件中连接起来,创建一个文件作为输出。因此,假设有两个与上面所述完全相同的文件,那么我的输出文件将包含:

    [{
            "Domain": "example.com",
            "A": ["50.63.202.28"],
            "MX": ["0 example-com.mail.protection.outlook.com."],
            "NS": ["ns48.example.com.", "ns47.example.com."],
            "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
            "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -all\""],
            "Country": ["United States"],
            "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
            "SSL": ["None"],
            "WHOIS": [1096],
            "TTL": ["568"]
        }, {
            "Domain": "example.org",
            "A": ["50.63.202.59"],
            "MX": ["30 ALT2.ASPMX.L.GOOGLE.COM.", "20 ALT1.ASPMX.L.GOOGLE.COM.", "50 ASPMX3.GOOGLEMAIL.COM.", "10 ASPMX.L.GOOGLE.COM.", "40 ASPMX2.GOOGLEMAIL.COM."],
            "NS": ["ns13.example.com.", "ns14.example.com."],
            "SOA": ["ns13.example.com. dns.jomax.net. 2016081700 28800 7200 604800 600"],
            "Country": ["United States"],
            "Hostname": ["'ip-50-63-202-59.ip.secureserver.net'"],
            "SSL": ["None"],
            "WHOIS": [5844],
            "TTL": ["569"]
        }, {
            "Domain": "example.com",
            "A": ["50.63.202.28"],
            "MX": ["0 example-com.mail.protection.outlook.com."],
            "NS": ["ns48.example.com.", "ns47.example.com."],
            "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
            "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -all\""],
            "Country": ["United States"],
            "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
            "SSL": ["None"],
            "WHOIS": [1096],
            "TTL": ["568"]
        }, {
            "Domain": "example.org",
            "A": ["50.63.202.59"],
            "MX": ["30 ALT2.ASPMX.L.GOOGLE.COM.", "20 ALT1.ASPMX.L.GOOGLE.COM.", "50 ASPMX3.GOOGLEMAIL.COM.", "10 ASPMX.L.GOOGLE.COM.", "40 ASPMX2.GOOGLEMAIL.COM."],
            "NS": ["ns13.example.com.", "ns14.example.com."],
            "SOA": ["ns13.example.com. dns.jomax.net. 2016081700 28800 7200 604800 600"],
            "Country": ["United States"],
            "Hostname": ["'ip-50-63-202-59.ip.secureserver.net'"],
            "SSL": ["None"],
            "WHOIS": [5844],
            "TTL": ["569"]
        }
    ]
    

    如何在不序列化(因为对象变化很大,我希望保留某种格式)和合并的情况下实现这一点?我试过 NewtonSoft's merge ,但对于这样简单的操作来说,似乎太复杂了。字符串操作是我最后一个选择吗(不知怎么的,这感觉不对,所以这个问题)?

    注: 虽然示例只显示了2个文件,但我将在一次go中合并至少100个文件。

    2 回复  |  直到 8 年前
        1
  •  2
  •   tym32167    8 年前

    我们需要有清除括号的函数

    private string RemoveBrackets(string content)
    {
        var openB = content.IndexOf("[");
        content = content.Substring(openB + 1, content.Length - openB - 1);
    
        var closeB = content.LastIndexOf("]");
        content = content.Substring(0, closeB);
    
        return content;
    }
    

    合并json的函数

    private string MergeJsons(string[] jsons)
    {
        var sb = new StringBuilder();
    
        sb.AppendLine("[");     
        for(var i=0; i<jsons.Length; i++)   
        {
            var json = jsons[i];
            var cleared = RemoveBrackets(json);
            sb.AppendLine(cleared);
            if (i != jsons.Length-1) sb.Append(",");
        }
    
        sb.AppendLine("]");     
        return sb.ToString();
    }
    

    样本数据

    string fileContent = @"[{
        ""Domain"": ""example.com"",
        ""A"": [""50.63.202.28""],
        ""MX"": [""0 example-com.mail.protection.outlook.com.""],
        ""NS"": [""ns48.example.com."", ""ns47.example.com.""],
        ""SOA"": [""ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600""],
        ""TXT"": [""\""MS=ms94763887\"""", ""\""google-site-verification=example-f0KFEgl-HnJF4_Gk\"""", ""\""v=spf1 include:spf.protection.outlook.com -all\""""],
        ""Country"": [""United States""],
        ""Hostname"": [""'ip-50-63-202-28.ip.secureserver.net'""],
        ""SSL"": [""None""],
        ""WHOIS"": [1096],
        ""TTL"": [""568""]
    }
    ]";
    

    使用示例

    var files = Enumerable.Range(1, 3).Select(x=>fileContent).ToArray();        
    Console.WriteLine(MergeJsons(files));
    

    结果

    [
    {
            "Domain": "example.com",
            "A": ["50.63.202.28"],
            "MX": ["0 example-com.mail.protection.outlook.com."],
            "NS": ["ns48.example.com.", "ns47.example.com."],
            "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
            "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -  all\""],
            "Country": ["United States"],
            "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
            "SSL": ["None"],
            "WHOIS": [1096],
            "TTL": ["568"]
        }  
    ,{
            "Domain": "example.com",
            "A": ["50.63.202.28"],
            "MX": ["0 example-com.mail.protection.outlook.com."],
            "NS": ["ns48.example.com.", "ns47.example.com."],
            "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
            "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -  all\""],
            "Country": ["United States"],
            "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
            "SSL": ["None"],
            "WHOIS": [1096],
            "TTL": ["568"]
        }  
    ,{
            "Domain": "example.com",
            "A": ["50.63.202.28"],
            "MX": ["0 example-com.mail.protection.outlook.com."],
            "NS": ["ns48.example.com.", "ns47.example.com."],
            "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
            "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -  all\""],
            "Country": ["United States"],
            "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
            "SSL": ["None"],
            "WHOIS": [1096],
            "TTL": ["568"]
        }  
    ]
    
        2
  •  0
  •   Legion    8 年前

    我不太明白你的意思,但似乎你需要的所有数据都是“数组”格式,所以使用字符串操作你只需要做这样的事情:

    读取第一个文件(USA A for with a counter that let you know what is the first file),将最后一个字符替换为“,”,并添加整个第二个文件,将第一个字符替换为“”,最后一个字符替换为“,”。

    对所有文件执行第二个文件中的操作,完成后将最后一个字符替换为“]”,并且有一个可以由json序列化程序读取的json内容数组。

    cose应该是这样的(此时在记事本上编写,不打开visual studio,因此可能包含一些数字错误):

    string my_big_json = string.empty;
    for (int a = 0; a < allJSONFiles.Lenght; a++)
    {
        StreamReader sr = new StreamReader(allJSONFiles[a]);
        string temp_data = sr.ReadToEnd().TrimEnd().TrimStart();
        sr.Close();
        if(a = 0)
        {
            temp_data = temp_data.Substring(0,temp_data.Lenght -1) + ",";
        }
        else
        {
            temp_data = temp_data.Substring(1,temp_data.Lenght -1) + " ";
            if(a < allJSONFiles.LenghtallJSONFiles.Lenght -1)
                temp_data = temp_data.Substring(0,temp_data.Lenght -1) + ",";
        }
        my_big_json += temp_data;
    }