I am working with JSON API data in .NET6 and was having trouble de-serializing nested data that has a variable rootname since my class type will never match what's in the JSON string.
{
"???":{
"type":"static",
"description":"n/a",
"id":null,
"upc":"s324df",
"price":1802
}
}
in the example above "???" equates to whatever is sent in the request so since I know the value, I just modified the JSON string and removed the root and the last "}" so that JsonSerializer will recognize the property names and is no longer looking for a root class with a type of "???". Here is my workaround code:
var response = await _client.GetAsync(api + Symbol);
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
var temp = result.Remove(0, Symbol.Count() + 4);
var temp2 = temp.Remove(temp.Count() - 1, 1);
quote = JsonSerializer.Deserialize(temp2, options);
}
I think this will work fine for what this application is, but feels janky and I am interested learning if there is a better way.