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

如何在C# ASP.NET中使用foreach循环读取JSON数据?

在C# ASP.NET中使用foreach循环读取JSON的education字段数据

针对你的需求,这里提供两种常用实现方式,可根据场景选择:

方式一:直接使用JObject遍历(无需定义模型)

基于你已有的代码,直接从反序列化后的JObject中提取education数组并遍历:

var response = client.Post(request);
var json = response.Content.ToString();
var data = (JObject)JsonConvert.DeserializeObject(json);

// 从data中取出education数组
var educationArray = data["data"]["education"] as JArray;

// 判空后开始遍历
if (educationArray != null)
{
    foreach (var educationItem in educationArray)
    {
        // 读取基础字段
        int id = educationItem["id"].Value<int>();
        string organization = educationItem["organization"].Value<string>();
        
        // 读取嵌套的accreditation信息
        string degree = educationItem["accreditation"]["education"].Value<string>();
        string educationLevel = educationItem["accreditation"]["educationLevel"].Value<string>();
        
        // 读取地点信息
        string formattedLocation = educationItem["location"]["formatted"].Value<string>();
        
        // 读取就读时间
        string startDate = educationItem["dates"]["startDate"].Value<string>();
        string completionDate = educationItem["dates"]["completionDate"].Value<string>();
        
        // 此处可根据业务需求处理数据,比如存入数据库、输出日志等
        Debug.WriteLine($"学校:{organization},学位:{degree},就读时间:{startDate} - {completionDate}");
    }
}

方式二:使用强类型模型遍历(类型安全,适合复杂场景)

如果后续需要频繁操作该JSON结构,定义对应模型类会让代码更规范、易维护:

第一步:定义匹配JSON结构的模型类

// 嵌套模型类
public class Accreditation
{
    public string education { get; set; }
    public string educationLevel { get; set; }
    public string inputStr { get; set; }
    public string matchStr { get; set; }
}

public class Location
{
    public string formatted { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    // 其他字段可按需添加
}

public class Dates
{
    public string startDate { get; set; }
    public string completionDate { get; set; }
    public bool isCurrent { get; set; }
    public string rawText { get; set; }
}

public class Education
{
    public int id { get; set; }
    public string organization { get; set; }
    public Accreditation accreditation { get; set; }
    public Location location { get; set; }
    public Dates dates { get; set; }
}

public class RootData
{
    public List<Education> education { get; set; }
    // 其他字段如certifications、dateOfBirth可按需添加
}

public class RootObject
{
    public RootData data { get; set; }
}

第二步:反序列化并遍历

var response = client.Post(request);
var json = response.Content.ToString();

// 反序列化为强类型对象
var rootObj = JsonConvert.DeserializeObject<RootObject>(json);

// 判空后遍历education列表
if (rootObj?.data?.education != null)
{
    foreach (var education in rootObj.data.education)
    {
        // 直接通过属性访问数据,类型安全
        Debug.WriteLine($"ID: {education.id}");
        Debug.WriteLine($"学校:{education.organization}");
        Debug.WriteLine($"学位:{education.accreditation.education}");
        Debug.WriteLine($"地点:{education.location.formatted}");
        Debug.WriteLine($"就读时间:{education.dates.startDate} - {education.dates.completionDate}");
    }
}

注意事项

  • 确保你的项目已安装Newtonsoft.Json包(可通过NuGet搜索安装)
  • 若JSON中存在可能为null的字段,建议在模型中使用可空类型(如string?、int?)避免空引用异常
  • 遍历前务必判空,防止数组为null时引发报错

内容的提问来源于stack exchange,提问作者Zahid Lateef

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 00:45:16