简体   繁体   English

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

[英]Merging Multiple JSON Files into Single File

I am reading Multiple JSON Files as such: 我正在读取多个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";
        }                   
    }
}

Now the JSON files more or less look like this, with multiple objects: 现在,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"]
    }
]

All I want to do is to join these objects from the multiple files I am reading to create a single file as output. 我要做的就是从正在读取的多个文件中加入这些对象,以创建一个文件作为输出。 So assuming that there were 2 exactly same files as stated above, my output file would contain: 因此,假设有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"]
    }
]

How do I achieve this without serializing (as the objects vary significantly and I want to retain the format somewhat) and merging? 我如何在不进行序列化的情况下实现此目标(因为对象差异很大,并且我希望保留某种格式)并合并? I tried NewtonSoft's merge , but it seems too complicated for such simple operation. 我尝试了NewtonSoft的merge ,但是对于这样简单的操作而言似乎太复杂了。 Is string manipulation my last option (somehow that doesn't feel correct, hence this question)? 字符串操作是我的最后选择吗(以某种方式感觉不正确,因此是这个问题)?

Note: While the example just shows 2 files, I will be merging atleast 100 files in a go. 注意:虽然该示例仅显示了2个文件,但我将一并合并至少100个文件。

We will need to have function which will clear brackets 我们将需要具有清除括号的功能

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

function to merge jsons 合并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();
}

sample data 样本数据

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

Example of usage 使用例

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

Results 结果

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

I'm not sure i understand what you mean, but seems that all the data you need is just into an "array-like" format, so using string manipulation you only need to do something like that: 我不确定我是否理解您的意思,但是似乎您需要的所有数据都只是呈“类似数组”的格式,因此使用字符串操作只需要执行以下操作:

Read First file (usa a for with a counter that let you know what is the first file), replace last charcater with "," and add the whole 2nd file replacing first character with " " and last character with ",". 读取第一个文件(带有用于让您知道第一个文件的计数器的for的a),将最后一个字符替换为“”,然后添加整个第二个文件,将第一个字符替换为“”,将最后一个字符替换为“,”。

Do the stuff of the 2nd file for all your files, when you finish replace last character with "]" and you've a single array of json stuff that can be read by a json serializer. 对所有文件执行第二个文件中的内容,完成后,将最后一个字符替换为“]”,并且您可以通过json序列化程序读取一个json内容数组。

The cose should be something like that (write at the moment on notepad, without visual studio opened, so it could contain some digit error): 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;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM