Reputation: 247
I am trying to serialize complex JSON object using C#. I got an initial output through my code.But I want to add another object in an array form before my serialized json. I created C# class from my json object in this way-
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; }
}
My code to get all stored .json file and then serialization is-
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);
}
}
But after running the code I am getting my output in this way-
{
"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"
]
}
]
}
But I also want to add "City" before this json and keep it an array form. My expected result is-
{
"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"
]
}
]
}
]
}
I am trying but could not get my result.I know that I also need to deserialize RootObject. But could no get how to do it here.I want to know what should I modify in my code to get this result.
Upvotes: 3
Views: 8507
Reputation: 9499
You are not writing out the RootObject in your code. Also you are writing out only one city instead of the list of cities
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);
}
Do this to fix the problem
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 } } );
}
basically we write a root object so that you get the "city" JSON field and also, we write a list of cities so that you get the array of cities as JSON.
Change the casing of "city" field name in "RootObject" class to get the right JSON value. Alternatively, you can also use the JsonProperty attribute on the field to give a name different from your field name.
e.g.
public class RootObject
{
[JsonProperty("MyCityName")]
public List<City> city { get; set; }
}
Upvotes: 2