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

时间:2018-06-08 09:44:54

标签: c# json

我正在阅读多个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个文件,但我将一次性合并至少100个文件。

2 个答案:

答案 0 :(得分:2)

我们需要具有清除括号的功能

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;
}

合并jsons的功能

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"]
    }  
]

答案 1 :(得分:0)

我不确定我理解你的意思,但似乎你需要的所有数据都只是一个“类似数组”的格式,所以使用字符串操作你只需要做类似的事情:

读取第一个文件(美国a用于计数器,让你知道第一个文件是什么),用“,”替换最后一个charcater,并添加整个第二个文件替换第一个字符用“”,最后一个字符用“,”

为你的所有文件做第二个文件的内容,当你用“]”替换最后一个字符时,你有一个可以被json序列化程序读取的json内容。

cose应该是那样的(写在记事本上的那一刻,没有打开视觉工作室,所以它可能包含一些数字错误):

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;
}