I have a key with random bits of string in it making it difficult to extract it's value. How would I go about doing it when I don't know the random bits ahead of time?
given:
sample = {\"total_ra4jhga987y3h_power\": 30}
Is it possible to do something directly like
dynamic obj = JsonConvert.DeserializeObject(sample);
obj[@"^total_(.*)_power$"] ---> should give me 30
Use LINQ to JSON instead. If you parse it as a JObject
, you can iterate over all the properties, and find those which match your regex.
Regex regex = new Regex(...);
JObject json = JObject.Parse(text);
foreach (var property in json.Properties())
{
if (regex.IsMatch(property.Name))
{
...
}
}
See more on this question at Stackoverflow