使用 C# 进行复杂 JSON 对象序列化
Complex JSON object Serialization using C#
我正在尝试使用 C# 序列化复杂的 JSON 对象。我通过 code.But 获得了初始输出 我想在序列化 json 之前以数组形式添加另一个对象。我以这种方式从我的 json 对象创建了 C# class-
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"
]
}
]
}
]
}
我正在尝试但无法让我的 result.I 知道我还需要反序列化 RootObject。但是不知道如何去做 here.I 想知道我应该在我的代码中修改什么才能得到这个结果。
您没有在代码中写出 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" class 中 "city" 字段名称的大小写以获得正确的 JSON 值。或者,您也可以使用字段上的 JsonProperty 属性来提供与您的字段名称不同的名称。
例如
public class RootObject
{
[JsonProperty("MyCityName")]
public List<City> city { get; set; }
}
我正在尝试使用 C# 序列化复杂的 JSON 对象。我通过 code.But 获得了初始输出 我想在序列化 json 之前以数组形式添加另一个对象。我以这种方式从我的 json 对象创建了 C# class-
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"
]
}
]
}
]
}
我正在尝试但无法让我的 result.I 知道我还需要反序列化 RootObject。但是不知道如何去做 here.I 想知道我应该在我的代码中修改什么才能得到这个结果。
您没有在代码中写出 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" class 中 "city" 字段名称的大小写以获得正确的 JSON 值。或者,您也可以使用字段上的 JsonProperty 属性来提供与您的字段名称不同的名称。
例如
public class RootObject
{
[JsonProperty("MyCityName")]
public List<City> city { get; set; }
}