You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中如何访问反序列化JSON内phonetics数组的audio及meanings下对象

解决方案

根因分析

你当前定义的DictionaryAPIResultData类结构与目标API返回的JSON结构不匹配:

  • phonetics字段并不是字符串数组,而是包含text(音标文本)、audio(音频地址)属性的对象数组
  • 你没有定义meanings相关的类结构,自然无法读取该节点下的词性、释义、例句等内容

第一步:定义完整的匹配类结构

using Newtonsoft.Json;
using System.Collections.Generic;

public class DictionaryAPIResultData
{
    [JsonProperty("word")]
    public string Word { get; set; }
    
    [JsonProperty("phonetic")]
    public string GlobalPhonetic { get; set; }
    
    [JsonProperty("phonetics")]
    public List<PhoneticItem> Phonetics { get; set; }
    
    [JsonProperty("meanings")]
    public List<MeaningItem> Meanings { get; set; }
}

public class PhoneticItem
{
    [JsonProperty("text")]
    public string PhoneticText { get; set; }
    
    [JsonProperty("audio")]
    public string AudioUrl { get; set; }
}

public class MeaningItem
{
    [JsonProperty("partOfSpeech")]
    public string PartOfSpeech { get; set; }
    
    [JsonProperty("definitions")]
    public List<DefinitionItem> Definitions { get; set; }
}

public class DefinitionItem
{
    [JsonProperty("definition")]
    public string Content { get; set; }
    
    [JsonProperty("example")]
    public string ExampleSentence { get; set; }
}

第二步:调整字段读取逻辑

private void Button1_Click(object sender, EventArgs e)
{
    string link = "https://api.dictionaryapi.dev/api/v2/entries/en/hello";
    string json = new WebClient().DownloadString(link);
    var resultList = JsonConvert.DeserializeObject<List<DictionaryAPIResultData>>(json);
    
    foreach (var wordData in resultList)
    {
        // 读取基础字段
        Console.WriteLine($"查询单词:{wordData.Word}");
        Console.WriteLine($"通用音标:{wordData.GlobalPhonetic}");

        // 读取phonetics数组的audio字段
        Console.WriteLine("\n==== 音标&音频列表 ====");
        foreach (var phonetic in wordData.Phonetics)
        {
            if (!string.IsNullOrWhiteSpace(phonetic.AudioUrl))
            {
                Console.WriteLine($"音标:{phonetic.PhoneticText},音频地址:{phonetic.AudioUrl}");
            }
        }

        // 读取meanings节点内容
        Console.WriteLine("\n==== 词义列表 ====");
        foreach (var meaning in wordData.Meanings)
        {
            Console.WriteLine($"【词性】{meaning.PartOfSpeech}");
            foreach (var def in meaning.Definitions)
            {
                Console.WriteLine($"释义:{def.Content}");
                if (!string.IsNullOrWhiteSpace(def.ExampleSentence))
                {
                    Console.WriteLine($"例句:{def.ExampleSentence}");
                }
            }
        }
    }
}

补充说明

  • WebClient在.NET Core/.NET 5+版本中已被标记为过时,生产环境建议替换为HttpClient避免性能问题
  • 部分phonetics项可能没有音频地址、部分释义可能没有例句,访问前做空判断可避免空引用异常
  • 如果你不需要API返回的全部字段,可以只保留需要的属性,Json.NET会自动忽略未定义的返回字段

内容的提问来源于stack exchange,提问作者Tim Birtles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 13:24:02