0

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..

1
  • 2
    Don't "split" it. Parse it with a well-known json-parser and take what you need. Commented Feb 3, 2023 at 10:28

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.