I have this API Response as a string.
/subscriptions/5e5c4cca-75b4-412d-96a1-45a9446ef08c/resourcegroups/ft-us-point-dev/providers/microsoft.datafactory/factories/ftadfqpb/providers/Microsoft.ResourceHealth/availabilityStatuses/current
Response object look like this :
{
"id": "/subscriptions/5e5c4cca-75b4-412d-96a1-45a9446ef08c/resourcegroups/ft-us-point-dev/providers/microsoft.purview/accounts/ft-ue-pdc-dev-purview/providers/Microsoft.ResourceHealth/availabilityStatuses/current",
"name": "current",
"type": "Microsoft.ResourceHealth/AvailabilityStatuses",
"location": "eastus",
"properties": {
"availabilityState": "Unknown",
"title": "Unknown",
"summary": "We are currently unable to determine the health of this Azure Purview.",
"reasonType": "",
"occuredTime": "2022-05-24T08:10:58.4372995Z",
"reasonChronicity": "Transient",
"reportedTime": "2022-05-24T08:10:58.4372995Z"
}
Now, I need each and every value from this response. For Example, subscriptions value as 5e5c4cca-75b4-412d-96a1-45a9446ef08c, resourcegroups value as ft-us-point-dev, providers value as microsoft.datafactory, factories value as ftadfqpb
How can I store these value so if in future if the api response has one or more values , my code is not affected by that.
CodePudding user response:
CodePudding user response:
var responseId = "/subscriptions/5e5c4cca-75b4-412d-96a1-45a9446ef08c/resourcegroups/ft-us-point-dev/providers/microsoft.purview/accounts/ft-ue-pdc-dev-purview/providers/Microsoft.ResourceHealth/availabilityStatuses/current";
var parts = responseId.Substring(1).Split("/");
var results = new Dictionary<string, string>();
for(int keyIdx = 0; keyIdx < parts.Length; keyIdx = 2)
{
if(!results.ContainsKey(parts[keyIdx]))
results.Add(parts[keyIdx], parts[keyIdx 1]);
}
- Either call
.Split('/').Skip(1)
or.Substring(1).Split('/')
to get rid of the leading/
- Iterate through the
parts
by incrementing the loop variable with 2 - If the key is already present ignore that key-value pair
- Otherwise put the key value into the
results
collection