I'm making some assumptions about how your data is structured. Also, you really shouldn't build JSON manually, which you appear to be doing at the end there. Instead, I'm returning a KeyValuePair
that can be added to a dictionary for serialization.
private static KeyValuePair<string, string> GetProperty(JObject objects,
string propertyKey, params string[] propertyNames)
{
JToken token = objects[propertyNames.First()];
foreach (var name in propertyNames.Skip(1))
token = token[0][name];
return new KeyValuePair<string, string>(propertyKey, (string)token);
}
// returns [Name, TEST]