Home > Software design >  how to split the json value using c#
how to split the json value using c#

Time:02-03

I am having a JSON file like this:

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

here how to split the district - > id which has value "3650117d-e60e-4b1d-897e-df5d6728a6a6"

thanks..

CodePudding user response:

You can parse it using Newtonsoft from Nuget. 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
    }
  ]
}";

Output:

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