【问题标题】:how to split the json value using c#如何使用 C# 拆分 json 值
【发布时间】:2023-02-03 18:46:31
【问题描述】:

我有一个这样的 JSON 文件:

  "{\"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}]}"

这里如何拆分区域 - > id 其值为“3650117d-e60e-4b1d-897e-df5d6728a6a6”

谢谢..

【问题讨论】:

  • 不要“分裂”它。用著名的 json-parser 解析它并取你需要的东西。

标签: c# .net .net-core


【解决方案1】:

您可以使用 Nuget 的 Newtonsoft 解析它。 IE:

void Main()
{
    var myJSON = JsonConvert.DeserializeAnonymousType(json, new { districts = new[] { new { id = Guid.NewGuid() }}});
    
    foreach (var x in myJSON.districts)
    {
        Console.WriteLine(x.id);
    }
}

static readonly string json = @"{
  ""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
    }
  ]
}";

输出:

3650117d-e60e-4b1d-897e-df5d6728a6a6
00000000-0000-0000-0000-000000000000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2017-10-29
    相关资源
    最近更新 更多