我正在读取多个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个文件。