C# 将多个JSON文件合并为单个文件
我正在读取多个JSON文件,例如:C# 将多个JSON文件合并为单个文件,c#,json,C#,Json,我正在读取多个JSON文件,例如: using (var fbd = new FolderBrowserDialog()) { DialogResult result = fbd.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath)) { IEnumerable<
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";
}
}
}
我所要做的就是从我正在读取的多个文件中连接这些对象,创建一个文件作为输出。因此,假设有2个与上述完全相同的文件,我的输出文件将包含:
[{
"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"]
}
]
如何在不序列化(因为对象差异很大,我希望保留某种格式)和合并的情况下实现这一点?我试过了,但对于这么简单的操作来说,它似乎太复杂了。字符串操作是我的最后一个选择吗(不知何故,这感觉不正确,因此产生了这个问题)
注意:虽然示例仅显示了2个文件,但我将一次合并至少100个文件。我们需要一个清除括号的函数
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();
}
用法示例
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"]
}
]
我们需要一个清除括号的函数
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();
}
用法示例
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"]
}
]
我不确定我是否理解您的意思,但似乎您需要的所有数据都是“类似数组”的格式,因此使用字符串操作您只需执行以下操作: 读取第一个文件(usa for,带有一个计数器,让您知道什么是第一个文件),将最后一个字符替换为“,”,并添加整个第二个文件,将第一个字符替换为“”,最后一个字符替换为“,” 当您完成将最后一个字符替换为“]”时,为您的所有文件执行第二个文件的内容,您就有了一个json序列化程序可以读取的json内容数组 cose应该是这样的(此时在记事本上书写,没有打开visual studio,因此可能包含一些数字错误):
string my_big_json=string.empty;
for(int a=0;a
我不确定我是否理解您的意思,但似乎您需要的所有数据都是“类似数组”的格式,因此使用字符串操作,您只需执行以下操作:
读取第一个文件(usa for,带有一个计数器,让您知道什么是第一个文件),将最后一个字符替换为“,”,并添加整个第二个文件,将第一个字符替换为“”,最后一个字符替换为“,”
当您完成将最后一个字符替换为“]”时,为您的所有文件执行第二个文件的内容,您就有了一个json序列化程序可以读取的json内容数组
cose应该是这样的(此时在记事本上书写,没有打开visual studio,因此可能包含一些数字错误):
string my_big_json=string.empty;
for(int a=0;a
非常感谢您这么做!谢谢你给我这么多!