I have a model to which the response from the API is written. Here is what it looks like
public class SparkPostTemplateModel
{
public List<SparkPostTemplateDetailModel> Results { get; set; }
}
And SparkPostTemplateDetailModel:
public class SparkPostTemplateDetailModel
{
public string Id { get; set; }
}
How can I save to JSON file each Id I got from an API?
This Is what I have got:
private SparkPostTemplateModel EmailTemplates { get; set; } = new();
public async Task SaveTemplate()
{
try
{
EmailTemplates = await SparkPostManager.GetTemplates();
var test = EmailTemplates.Results.ToList();
string fileName = "response.json";
using FileStream createStream = File.Create(fileName);
await JsonSerializer.SerializeAsync(createStream, EmailTemplates);
await createStream.DisposeAsync();
File.WriteAllText(@"C:\temp\Response.json", test.ToString());
Console.WriteLine(File.ReadAllText(fileName));
}
catch (Exception ex)
{
throw;
}
And what my program save to file: System.Collections.Generic.List`1[Test.API.Client.Infrastructure.Models.SparkPostTemplateDetailModel]
I want it to look like this:
Id1
Id2
Id3
Thanks for any tips!