0

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!

Tim Birtles
  • 147
  • 1
  • 2
  • 9
  • i suggest you to use a n mp3 streamer like NAUDIO for example, i dont think the c# sound player read mp3, only wav – Frenchy Nov 25 '21 at 16:07
  • Btw, if you have JSON and you don't know how to design your container classes around it there are some useful tools which can do this for you. E.g: https://json2csharp.com/ – croxy Nov 25 '21 at 16:40

1 Answers1

0

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#? ...

Mucksh
  • 160
  • 5