I've added Newtonsoft.Json to my project.
I'm calling the lyric API at https://apiseeds.com/documentation/lyrics with the following code:
LyricClient = new HttpClient
{
BaseAddress = new Uri("https://orion.apiseeds.com/api/music/lyric")
};
LyricClient.DefaultRequestHeaders.Accept.Clear();
LyricClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string url = LyricClient.BaseAddress + "/" + artist + "/" + track;
var result = await LyricClient.GetAsync(url + "?apikey=" + ApiSeedsKey);
if (result.IsSuccessStatusCode)
{
var resultString = await result.Content.ReadAsStringAsync();
}
and getting a result string with all the fields named and with the appropriate values (formatted for clarity):
{
"result":
{
"artist":
{
"name": "Marianne Faithfull"
},
"track":
{
"name": "Broken English",
"text": "[Verse 1]...",
"lang":
{
"code": "en",
"name": "English"
}
},
"copyright":
{
"notice": "Broken English lyrics are property and copyright of their owners. Commercial use is not allowed.",
"artist": "Copyright Marianne Faithfull",
"text": "All lyrics provided for educational purposes and personal use only."
},
"probability": 100,
"similarity": 1
}
}
So far, so good.
However, when I try to convert it into a C# object so I can manipulate/display it in my program I'm not getting any data back.
I've created a Result
object
public class Result
{
public Artist Artist { get; set; }
public Track Track { get; set; }
public Copyright Copyright { get; set; }
public double Probability { get; set; }
public double Similarity { get; set; }
}
and the other sub objects with properties to match the names in the Json to hold this data, for example:
public class Artist
{
public string Name { get; set; }
}
etc.
However, when I call
var resultObject = JsonConvert.DeserializeObject<Result>(resultString);
I get a Result
object, but all the properties are set to null or 0.
I've tried making the property names lower case and adding the [DataContract]
and [DataMember]
attributes to the classes, but that's had no effect on the results. I've checked for examples and the ones at places like
https://www.c-sharpcorner.com/article/json-serialization-and-deserialization-in-c-sharp/
and
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to
don't appear to contradict what I've done, but I still can't get it to work. I must be missing something obvious in my class declarations, but I can't work out what.