从文件夹中获取所有 .json 文件,然后使用 C# 和 JSON.Net 序列化为单个 .Json 文件并序列化

2 c# serialization jsonserializer json.net deserialization

我有很多 .json 文件,它们存储在一个文件夹中。现在我想将所有这些 .json 文件序列化到一个文件中。好吧,我开始如何继续它,但没有得到正确的方法。我的单个 .json 文件如下所示-

\n\n
{\n      "Name": "Holstentor",\n      "Shorttext": "The Holsten Gate is a city gate marking off the western boundary of the old center of the Hanseatic city of L\xc3\xbcbeck. This Brick Gothic construction is one of the relics of L\xc3\xbcbeck\xe2\x80\x99s medieval city fortifications and one of two remaining city gates, the other being the Citadel Gate  Because its two round towers and arched entrance are so well known it is regarded today as a symbol of this German city, and together with the old city centre of L\xc3\xbcbeck it has been a UNESCO World Heritage Site since 1987.\\nHolstentor was built in 1464.",\n      "GeoCoordinates": {\n        "Longitude": 10.6797,\n        "Latitude": 53.8662\n      },\n      "Images": [\n        "https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Holstentor_in_L%C3%BCbeck_Frontseite_-_Zuschnitt.jpg/378px-Holstentor_in_L%C3%BCbeck_Frontseite_-_Zuschnitt.jpg"\n      ]\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

和另一个 .json 文件

\n\n
{\n          "Name": "Passat (ship)",\n          "Shorttext": "Passat is a German four-masted steel barque and one of the Flying P-Liners, the famous sailing ships of the German shipping company F. Laeisz. The name \\"Passat\\" means trade wind in German. She is one of the last surviving windjammers.",\n          "GeoCoordinates": {\n            "Longitude": 10.88138889,\n            "Latitude": 53.95805556\n          },\n          "Images": [\n            "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Flying_P-Liner_Passat_ship_in_Travem%C3%BCnde.jpg/400px-Flying_P-Liner_Passat_ship_in_Travem%C3%BCnde.jpg"\n          ]\n        }\n
Run Code Online (Sandbox Code Playgroud)\n\n

我每个地方都有类似的 .json 文件 10 或 15。我将此文件存储在一个文件夹中,文件夹名称是主要城市名称。

\n\n

现在我想获取所有这些 .json 文件并将它们序列化在一个类似这样的文件中 -

\n\n

我已经以类似的方式为 json 对象生成了 C# 类 -

\n\n
 public class GeoCoordinates\n {\n public double Longitude { get; set; }\n public double Latitude { get; set; }\n }\n\npublic class Tourist\n {\n  public string Name { get; set; }\n  public string Shorttext { get; set; }\n  public GeoCoordinates GeoCoordinates { get; set; }\n  public List<string> Images { get; set; }\n }\n\n public class City\n {\n  public List<Tourist> Tourist { get; set; }\n }\n\npublic class RootObject\n{\n  public List<City> city { get; set; }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我这样盯着我的编码-

\n\n
 static void Main(string[] args)\n    {\n        var startPath = Application.StartupPath;\n        var cities = new List<City>();\n        DirectoryInfo d = new DirectoryInfo(startPath + @"\\FinalJson\\Flensburg\\");\n        foreach (var file in d.GetFiles("*.json"))\n        {\n          using (StreamReader fi = File.OpenText(file.FullName))\n          {\n            JsonSerializer serializer = new JsonSerializer();\n            City city = (City)serializer.Deserialize(fi, typeof(City));\n           cities.Add(city);\n         }\n        }\n\n        using (StreamWriter file = File.CreateText(@"c:\\cities.json"))\n        {\n            JsonSerializer serializer = new JsonSerializer();\n            serializer.Serialize(file, cities);\n        }\n\n\n\n    }\n\n    public virtual string FullName { get; set; }\n\n    static void DisplayFileSystemInfoAttributes(FileSystemInfo fi)\n    {\n        //  Assume that this entry is a file.\n        string entryType = "File";\n\n        // Determine if entry is really a directory\n        if ((fi.Attributes & FileAttributes.Directory) == FileAttributes.Directory)\n        {\n            entryType = "Directory";\n        }\n        //  Show this entry\'s type, name, and creation date.\n        Console.WriteLine("{0} entry {1} was created on {2:D}", entryType, fi.FullName, fi.CreationTime);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想以这种方式得到我的结果-

\n\n
{\n"Kiel": [ //city name\n    {\n        "Tourist": [\n            {\n                "Name": "Holstentor",\n                "Shorttext": "The Holsten Gate is a city gate marking off the western boundary of the old center of the Hanseatic city of L\xc3\xbcbeck. This Brick Gothic construction is one of the relics of L\xc3\xbcbeck\xe2\x80\x99s medieval city fortifications and one of two remaining city gates, the other being the Citadel Gate  Because its two round towers and arched entrance are so well known it is regarded today as a symbol of this German city, and together with the old city centre of L\xc3\xbcbeck it has been a UNESCO World Heritage Site since 1987.\\nHolstentor was built in 1464.",\n                "GeoCoordinates": {\n                    "Longitude": 10.6797,\n                    "Latitude": 53.8662\n                },\n                "Images": [\n                    "https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Holstentor_in_L%C3%BCbeck_Frontseite_-_Zuschnitt.jpg/378px-Holstentor_in_L%C3%BCbeck_Frontseite_-_Zuschnitt.jpg"\n                ]\n            },\n {\n                "Name": "Stadion Lohm\xc3\xbchle",\n                "Shorttext": "Das Stadion an der Lohm\xc3\xbchle, oder auch einfach nur \xe2\x80\x9eLohm\xc3\xbchle\xe2\x80\x9c genannt, ist ein reines Fu\xc3\x9fballstadion in L\xc3\xbcbeck und das gr\xc3\xb6\xc3\x9fte Stadion in Schleswig-Holstein.\\nEs ist die Heimat des VfB L\xc3\xbcbeck. Nach Abriss der alten Trib\xc3\xbcne und dem Bau der neuen Haupttrib\xc3\xbcne in den 1990er Jahren im Zuge des Aufstiegs in die 2. Bundesliga im Jahre 1996 fasst das Stadion 17.869 Pl\xc3\xa4tze, darunter etwa 4.400 \xc3\xbcberdachte Sitzpl\xc3\xa4tze.\\n\\n",\n                "GeoCoordinates": {\n                    "Longitude": 10.66888905,\n                    "Latitude": 53.88111115\n                },\n                "Images": [\n                    "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/L%C3%BCbeck-Lohm%C3%BChle_1.jpg/400px-L%C3%BCbeck-Lohm%C3%BChle_1.jpg"\n                ]\n            },\n\n   //ans so on ..........\n\n        ]\n\n    }\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Muh*_*ais 5

这对我有用

    using System;
    using System.Collections.Generic;
    using Newtonsoft.Json;

    public class RootObject
    {
        public string url_short { get; set; }
        public string url_long { get; set; }
        public int type { get; set; }
    }

    public class Program
    {
        static public void Main()
        {
foreach (string fileName in Directory.GetFiles("directoryName", "*.json")
    {
       using (StreamReader r = new StreamReader(Server.MapPath(fileName)))
           {
               string json = r.ReadToEnd();
               List<RootObject> ro = JsonConvert.DeserializeObject<List<RootObject>>(json);
           }
        // Do something with the file content
    }

        }  
    }
Run Code Online (Sandbox Code Playgroud)