我有这段代码,它从这个api下载一个字符串,然后反序列化它。我已经知道了如何访问“单词”和“语音”对象,但是如何访问“语音”数组中的“音频”对象,或者“意义”中的一些对象呢?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; }
}希望有人能帮忙!
发布于 2021-11-25 16:11:59
JSON对象如下所示:
"phonetics":[{"text":"həˈləʊ","audio":"//ssl.gstatic.com/dictionary/static/sounds/20200429/hello--_gb_1.mp3"},{"text":"hɛˈləʊ"}]因此,只需添加另一个类来表示它,并将反序列化类更改为
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; }
}只有在类属性的名称与属性的名称不同时才需要使用JsonProperty属性,或者将类属性从小写改为大写
我第一次误解了它,所以如果你想播放声音如何从在线资源在.mp3中播放C#文件?,这里就是答案.
https://stackoverflow.com/questions/70114073
复制相似问题