I have this code, which downloads a string from this api, and then deserializes it. I have figured out how to access the "word" and "phonetic" objects, but how would I access the "audio" object inside of the "phonetics" array, or some of the objects inside of "meanings"? 1
private void Button1_Click(object sender, EventArgs e)
{
string result = "";
string link = ($"https://api.dictionaryapi.dev/api/v2/entries/en/hello");
var json = new WebClient().DownloadString(link);
var jsonDes = JsonConvert.DeserializeObject<List<DictionaryAPIResultData>>(json);
foreach (var data in jsonDes)
{
Console.WriteLine( data.phonetics);
}
}
public class DictionaryAPIResultData
{
[JsonProperty("word")]
internal string word { get; set; }
[JsonProperty("phonetics")]
internal List<string> phonetics { get; set; }
}
Hope someone can help!
CodePudding user response:
The JSON object looks like this:
"phonetics":[{"text":"həˈləʊ","audio":"//ssl.gstatic.com/dictionary/static/sounds/20200429/hello--_gb_1.mp3"},{"text":"hɛˈləʊ"}]
so just add another class to represent it and change your deserialization class to
public class DictionaryAPIResultData
{
[JsonProperty("word")]
internal string word { get; set; }
[JsonProperty("phonetics")]
internal List<Phonetics> phonetics { get; set; }
}
public class Phonetics
{
public string text { get; set; }
public string audio { get; set; }
}
the JsonProperty attribute do you only need if your class property has an different name than the property insinde the json, or if you change it from lower to upper case
I first misread it so here is the answer if you wanna play the sound How to play .mp3 file from online resources in C#? ...