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

如何使用C#提取JSON数据中district的id字段值

C#提取JSON中districts数组首个元素的id值

下面提供两种主流实现方式,覆盖原生框架和第三方库场景:

方式一:使用.NET原生System.Text.Json

1. 实体类映射(规范场景推荐)

先定义匹配JSON结构的实体类(仅保留需要的字段即可):

public class District
{
    [JsonPropertyName("id")]
    public string Id { get; set; }
}

public class RootResponse
{
    [JsonPropertyName("districts")]
    public List<District> Districts { get; set; }
}

执行解析逻辑:

// 替换为你的标准JSON内容(需去除外层转义引号,将\"替换为")
string jsonContent = @"{""page"":1,""limit"":10,""pages"":1,""total_results"":2,""districts"":[{""object"":""district"",""links"":[],""id"":""3650117d-e60e-4b1d-897e-df5d6728a6a6"",""name"":""Wake County Public School System"",""database_code"":""NC183"",""database_host"":""osp-tdb01"",""website"":""WakeNC"",""prefix"":""00183"",""state"":""NC"",""state_fips"":""00"",""county_code"":""183"",""short_county_code"":null,""salesforce_id"":null,""nces_id"":null,""fee_per_transaction_credit"":0.0,""fee_per_transaction_e_check"":0.0,""fee_percent_credit"":0.04,""fee_percent_e_check"":0.0,""fee_paid_by"":null},{""object"":""district"",""links"":[],""id"":""00000000-0000-0000-0000-000000000000"",""name"":"""",""database_code"":""NC183"",""database_host"":""OSP-DB01"",""website"":""WakeNC"",""prefix"":""37183"",""state"":""NC"",""state_fips"":""37"",""county_code"":""183"",""short_county_code"":null,""salesforce_id"":null,""nces_id"":null,""fee_per_transaction_credit"":0.0,""fee_per_transaction_e_check"":0.0,""fee_percent_credit"":0.0,""fee_percent_e_check"":0.0,""fee_paid_by"":null}]}";

var response = JsonSerializer.Deserialize<RootResponse>(jsonContent);
string firstDistrictId = response?.Districts?.FirstOrDefault()?.Id;
// 输出结果:3650117d-e60e-4b1d-897e-df5d6728a6a6

2. 无实体类直接解析(快速临时场景)

无需定义实体类,直接通过JsonDocument遍历节点:

string jsonContent = @"{""page"":1,""limit"":10,...}"; // 同上完整JSON

using JsonDocument doc = JsonDocument.Parse(jsonContent);
JsonElement root = doc.RootElement;

if (root.TryGetProperty("districts", out JsonElement districtsArray) && districtsArray.GetArrayLength() > 0)
{
    string firstDistrictId = districtsArray[0].GetProperty("id").GetString();
    // 使用提取到的id
}

方式二:使用Newtonsoft.Json(Json.NET)

如果项目依赖第三方库Json.NET,可按以下方式实现:

1. 实体类映射

public class District
{
    [JsonProperty("id")]
    public string Id { get; set; }
}

public class RootResponse
{
    [JsonProperty("districts")]
    public List<District> Districts { get; set; }
}

// 解析代码
string jsonContent = @"{""page"":1,""limit"":10,...}"; // 同上完整JSON
var response = JsonConvert.DeserializeObject<RootResponse>(jsonContent);
string firstDistrictId = response?.Districts?.FirstOrDefault()?.Id;

2. 动态类型解析

string jsonContent = @"{""page"":1,""limit"":10,...}";
dynamic jsonObj = JsonConvert.DeserializeObject(jsonContent);
string firstDistrictId = jsonObj.districts[0].id;

注意:如果你的JSON内容是带有外层转义引号的字符串(如原始输入格式),需先预处理:去除首尾双引号,再将所有\"替换为",得到标准JSON格式后再解析。

内容的提问来源于stack exchange,提问作者Vasanth R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 13:35:18