【发布时间】:2016-02-16 15:15:04
【问题描述】:
我正在尝试动态地从文件夹中获取所有 .json 文件,以将所有 .json 文件合并到一个 .json 文件中。但是在运行代码时,我得到文件未找到异常并且无法运行程序。我获取所有 .json 文件的代码是-
static void Main(string[] args)
{
var startPath = Application.StartupPath;
var cities = new List<City>();
DirectoryInfo d = new DirectoryInfo(startPath+@"\Flensburg\");
foreach (var file in d.GetFiles())
{
using (StreamReader fi = File.OpenText(file.Name)) //getting file not found exception
{
JsonSerializer serializer = new JsonSerializer();
City city = (City)serializer.Deserialize(fi, typeof(City));
cities.Add(city);
}
}
using (StreamWriter file = File.CreateText(@"C:\C# tutorial Backup\joint_josn\joint_josn\bin\Debug\cities.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, cities);
}
}
我的 json 对象类是-
public class GeoCoordinates
{
public double Longitude { get; set; }
public double Latitude { get; set; }
}
public class Tourist
{
public string Name { get; set; }
public string Shorttext { get; set; }
public GeoCoordinates GeoCoordinates { get; set; }
public List<string> Images { get; set; }
}
public class City
{
public List<Tourist> Tourist { get; set; }
}
public class RootObject
{
public List<City> city { get; set; }
}
我的一个json文件是这样的-
{
"Name": "Flensburg Firth",
"Shorttext": "Flensburg Firth or Flensborg Fjord is the westernmost inlet of the Baltic Sea. It forms part of the border between Germany to the south and Denmark to the north. Its length is either 40 or 50 km, depending to the definition of its limits. It has the largest surface of all Förden and East Jutland Fjorde, which are a special type of inlets, different from geological fjords.\nTwo peninsulas, Broager peninsula on the northern side and Holnis peninsula on the southern side divide the inlet in an outer and an inner part. West of them, near the Danish coast, there are two small Islands called Okseøer.\nOn the Danish side, outer part of the northern limits of the firth is formed by the island of Als with the town of Sønderborg. Towards the west, continuing on the Danish side are Broager, Egernsund, Gråsten, Rinkenæs, Sønderhav, and Kollund.\nIn Germany at the Danish border there is Harrislee, at the inner end of the inlet the town of Flensburg, east of it on the southern shore the town of Glücksburg and the villages Munkbrarup, Langballig, Westerholz, Quern, Steinberg, Niesgrau, Gelting, and Nieby.\n\n",
"GeoCoordinates": {
"Longitude": 9.42901993,
"Latitude": 54.7959404
},
"Images": [
"CE3222F5.jpg"
]
}
我还有更多的 json 文件,例如以下文件。
我想把所有的文件都这样序列化-
{
"Kiel": [ //city name
{
"Tourist": [
{
"Name": "Holstentor",
"Shorttext": "The Holsten Gate is a city gate marking off the western boundary of the old center of the Hanseatic city of Lübeck. This Brick Gothic construction is one of the relics of Lübeck’s 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übeck it has been a UNESCO World Heritage Site since 1987.\nHolstentor was built in 1464.",
"GeoCoordinates": {
"Longitude": 10.6797,
"Latitude": 53.8662
},
"Images": [
"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"
]
},
{
"Name": "Stadion Lohmühle",
"Shorttext": "Das Stadion an der Lohmühle, oder auch einfach nur „Lohmühle“ genannt, ist ein reines Fußballstadion in Lübeck und das größte Stadion in Schleswig-Holstein.\nEs ist die Heimat des VfB Lübeck. Nach Abriss der alten Tribüne und dem Bau der neuen Haupttribüne in den 1990er Jahren im Zuge des Aufstiegs in die 2. Bundesliga im Jahre 1996 fasst das Stadion 17.869 Plätze, darunter etwa 4.400 überdachte Sitzplätze.\n\n",
"GeoCoordinates": {
"Longitude": 10.66888905,
"Latitude": 53.88111115
},
"Images": [
"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"
]
},
//ans so on ..........
]
}
]
}
【问题讨论】:
-
我假设这只是一个错字,您并没有真正尝试在没有文件名的情况下直接写入启动路径。
-
@kjbartel:请您解释一下。在放置断点时,我只得到名字。但之后它显示文件未找到并且无法在其中获取任何值
-
这是一个用调试器发现这个错误的好机会。作为专业人士,您需要能够相当系统地发现这些简单的错误。
标签: c# json serialization exception-handling deserialization