I'm trying to serialize this json structure in c# and was wondering how would I serialize the address array in my structure below without having to use classes
Heres the structure
{
"Name":"Mark",
"Addresses":[
{
"address":"1234 Main street",
},
{
"address":"1234 Pine Street",
}],
}
Here's how I serialize using a JsonSerializer and only serializing one address
string jsonObject = JsonSerializer.Serialize(new
{
name = "Mark",
Addresses = new
{
address= "1234 Main street"
},
});
How can I serialize the other address
CodePudding user response:
You need to instantiate a collection type, such as an array:
string jsonObject = JsonSerializer.Serialize(new
{
name = "Mark",
Addresses = new[]
{
new { address = "1234 Main street" }
},
});