So basically I want to deserialize JSON data from sepcific URL to this class structure and then use it to display it on a page.
I don't understand how should I deserialize it because there are two classes that are compatible with JSON structure on the url.
This is the code shown below.
public class IndexModel : PageModel
{
public void OnGet()
{
Rootobject rootobject = new Rootobject();
rootobject.regions[0] = _download_serialized_json_data<Region>("https://visservice.meteoalarm.org/api/v1/regions?language=ATOM");
}
private static Region _download_serialized_json_data<Region>(string url) where Region : new()
{
using (var w = new WebClient())
{
var json_data = string.Empty;
// attempt to download JSON data as a string
try
{
json_data = w.DownloadString(url);
}
catch (Exception) { }
// if string with JSON data is not empty, deserialize it to class and return its instance
return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<Region>(json_data) : new Region();
}
}
public class Rootobject
{
public Region[] regions { get; set; }
}
public class Region
{
public bool active { get; set; }
public float[][] bb { get; set; }
public string code { get; set; }
public string name { get; set; }
}
}
Edit:
CodePudding user response:
As per your class definitoins, it should be JsonConvert.DeserializeObject<Rootobject>(json_data)