I have the following JSON string, and I need to access the property value named "Mode" from it:
{
"CommomData": {
"DateTime": {
"Year": 2019,
"Month": 3,
"Day": 11,
"Hour": 14,
"Min": 1,
"Second": 29
}
},
"Status": {
"Mode": "Test",
"Loc": "Test"
}
}
If you note here, the parent property name for "Mode", here is "Status" but this could change to "LatestStatus" or "FirstStatus" or any other value.
Currently, I have written the code below and it works fine, but it takes around 60 to 150 milliseconds for the operation (we want to reduce this time). Please note that the object has many more properties, but I only posted a few to explain the issue.
Is there any other optimal way to get the value from the JSON string without knowing the object type and the parent property name?
JObject payloadJObject = JObject.Parse(payload);
foreach (var item in payloadJObject)
{
foreach (JProperty subitem in item.Value.ToList())
{
if (subitem.Name == "Mode")
{
return Convert.ToString(subitem.Value);
}
}
}