I have a Json file as below
{
"objectId": "123",
"properties": {
"objectId": "456"
},
"variables": [
{
"objectId": "789"
},
{
"objectId": "012"
}
]
}
I want to get all 'objectId's in a list like [ "123", "456", "789", "012" ]
I tried as below
var body = JObject.Parse(jsonString); //using Newtonsoft library
var list = body.Properties().Where(p => p.Name == "objectId").Select(p => p.Value).ToList();
I tried in a below way too
var list = new List<string>();
foreach(var prop in body.Properties())
{
if(prop.Name == "objectId")
{
list.Add(prop.Value.ToString());
}
}
But here i get only first level properties.
body
? And have you tried deserializing to C# classes?