我正在尝试使用C#序列化复杂的JSON对象。我通过我的代码获得了初始输出。但是我想在序列化的json之前以数组形式添加另一个对象。我以这种方式从我的json对象创建了C#类 -
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文件然后序列化的代码是 -
static void Main(string[] args)
{
var startPath = Application.StartupPath;
var city = new City { Tourist = new List<Tourist>() };
DirectoryInfo d = new DirectoryInfo(startPath+@"\Flensburg\");
foreach (var file in d.GetFiles())
{
using (StreamReader fi = File.OpenText(file.FullName))
{
JsonSerializer serializer = new JsonSerializer();
Tourist tourist = (Tourist)serializer.Deserialize(fi, typeof(Tourist));
city.Tourist.Add(tourist);
}
}
using (StreamWriter file = File.CreateText(@"C:\C# tutorial Backup\joint_josn\joint_josn\bin\Debug\cities.json"))
{
JsonSerializer serializer = new JsonSerializer { Formatting = Formatting.Indented };
serializer.Serialize(file, city);
}
}
但是在运行代码后,我以这种方式获得输出 -
{
"Tourist": [
{
"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"
]
},
{
"Name": "Naval Academy Mürwik",
"Shorttext": "The Naval Academy Mürwik is the main training establishment for all German Navy officers and replaced the German Imperial Naval Academy in Kiel.\nIt is located at Mürwik which is a part of Germany's most northern city, Flensburg. Built on a small hill directly by the coast, it overlooks the Flensburg Fjord. The main building of the academy is known for its beautiful architecture and location, and is often named the \"Red Castle\".\n\n",
"GeoCoordinates": {
"Longitude": 9.45944444,
"Latitude": 54.815
},
"Images": [
"34AADEDE.jpg"
]
},
{
"Name": "Nordertor",
"Shorttext": "The Nordertor is an old town gate in Flensburg, Germany, which was built around 1595. Today the landmark is used as a symbol for Flensburg.\n\n",
"GeoCoordinates": {
"Longitude": 9.43004861,
"Latitude": 54.79541778
},
"Images": [
"D02DCA3E.jpg"
]
}
]
}
但是我也想在这个json之前添加“City”并保持一个数组形式。我的预期结果是 -
{
"City": [
{
"Tourist": [
{
"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"
]
},
{
"Name": "Naval Academy Mürwik",
"Shorttext": "The Naval Academy Mürwik is the main training establishment for all German Navy officers and replaced the German Imperial Naval Academy in Kiel.\nIt is located at Mürwik which is a part of Germany's most northern city, Flensburg. Built on a small hill directly by the coast, it overlooks the Flensburg Fjord. The main building of the academy is known for its beautiful architecture and location, and is often named the \"Red Castle\".\n\n",
"GeoCoordinates": {
"Longitude": 9.45944444,
"Latitude": 54.815
},
"Images": [
"34AADEDE.jpg"
]
},
{
"Name": "Nordertor",
"Shorttext": "The Nordertor is an old town gate in Flensburg, Germany, which was built around 1595. Today the landmark is used as a symbol for Flensburg.\n\n",
"GeoCoordinates": {
"Longitude": 9.43004861,
"Latitude": 54.79541778
},
"Images": [
"D02DCA3E.jpg"
]
}
]
}
]
}
我正在尝试但无法得到我的结果。我知道我还需要反序列化RootObject。但是我不知道如何在这里做。我想知道我应该在我的代码中修改什么来获得这个结果。
答案 0 :(得分:1)
您没有在代码中写出RootObject。 此外,您只会写出一个城市而不是城市列表
using (StreamWriter file = File.CreateText(@"C:\C# tutorial Backup\joint_josn\joint_josn\bin\Debug\cities.json"))
{
JsonSerializer serializer = new JsonSerializer { Formatting = Formatting.Indented };
serializer.Serialize(file, city);
}
执行此操作以解决问题
using (StreamWriter file = File.CreateText(@"C:\C# tutorial Backup\joint_josn\joint_josn\bin\Debug\cities.json"))
{
JsonSerializer serializer = new JsonSerializer { Formatting = Formatting.Indented };
serializer.Serialize(file, new RootObject { city = new List<City> { city } } );
}
基本上我们编写一个根对象,以便您获得“city”JSON字段,并且我们编写一个城市列表,以便您将城市数组作为JSON。
更改“RootObject”类中“city”字段名称的大小以获取正确的JSON值。或者,您也可以使用字段上的JsonProperty属性来提供与您的字段名称不同的名称。
e.g。
public class RootObject
{
[JsonProperty("MyCityName")]
public List<City> city { get; set; }
}