如何访问反序列化 json 中的 "text" 和 "audio" 对象?
How do I access the "text" and "audio" objects inside of a deserialized json?
我有这段代码,它从 this 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; }
}
希望有人能帮忙!
JSON 对象如下所示:
"phonetics":[{"text":"həˈləʊ","audio":"//ssl.gstatic.com/dictionary/static/sounds/20200429/hello--_gb_1.mp3"},{"text":"hɛˈləʊ"}]
所以只需添加另一个 class 来表示它并将反序列化 class 更改为
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; }
}
如果您的 class 属性 的名称与 json 中的 属性 的名称不同,或者如果您将其从较低的名称更改为大写
我第一次看错了,如果你想播放声音,这里是答案 How to play .mp3 file from online resources in C#? ...
我有这段代码,它从 this 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; }
}
希望有人能帮忙!
JSON 对象如下所示:
"phonetics":[{"text":"həˈləʊ","audio":"//ssl.gstatic.com/dictionary/static/sounds/20200429/hello--_gb_1.mp3"},{"text":"hɛˈləʊ"}]
所以只需添加另一个 class 来表示它并将反序列化 class 更改为
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; }
}
如果您的 class 属性 的名称与 json 中的 属性 的名称不同,或者如果您将其从较低的名称更改为大写
我第一次看错了,如果你想播放声音,这里是答案 How to play .mp3 file from online resources in C#? ...