I want extrac noeud of a json and return json value c# for exapmle:
i have this Json result :
{
"Features": {
"FeatureActive": true,
"FeatureRelanceAutoJSON": {
"RelanceAuto": {
"RelanceAutoParCategorie": [
{
"categorie": "platinum",
"statut": "Active",
"default": "Active"
},
{
"categorie": "gold",
"statut": "Active",
"default": "Inactive"
},
{
"categorie": "silver",
"statut": "Active",
"default": "Inactive"
}
]
}
}
},
"Data-Doc": {
"Doc-Siege": "Siège",
"Branche": "Branche",
"TypeDocumentsJSON": {
"TypeDocuments": [
{
"nomDocument": "Document Xml",
"typeDoc": "Xml",
"description": "Desc doc"
},
{
"nomDocument": "Document Pdf",
"typeDoc": "Pdf",
"description": "Desc doc"
},
{
"nomDocument": "Document Word",
"typeDoc": "Word",
"description": "Desc doc"
}
]
},
"PortefeuillesJSON": {
"portefeuilles": [
"12344",
"90871",
"12873",
"30920"
]
}
},
"Metadata": {
"name":"Sunshine Department Store",
"address":"Wangfujing Street",
"cod": "200",
"message": "0.0284"
}
}
And i want to extacte noeud of this Json.
example i give :
FeatureRelanceAutoJSON
and i have a result like :
"RelanceAuto": {
"RelanceAutoParCategorie": [
{
"categorie": "platinum",
"statut": "Active",
"default": "Active"
},
{
"categorie": "gold",
"statut": "Active",
"default": "Inactive"
},
{
"categorie": "silver",
"statut": "Active",
"default": "Inactive"
}
]
}
What I have tried:
i have tried this solution on :
c# - Searching for a specific JToken by name in a JObject hierarchy - Stack Overflow[
^]
but i always get count 0.
i have had this class :
public static class JsonExtensions
{
public static List<JToken> FindTokens(this JToken containerToken, string name)
{
List<JToken> matches = new List<JToken>();
FindTokens(containerToken, name, matches);
return matches;
}
private static void FindTokens(JToken containerToken, string name, List<JToken> matches)
{
if (containerToken.Type == JTokenType.Object)
{
foreach (JProperty child in containerToken.Children<JProperty>())
{
if (child.Name == name)
{
matches.Add(child.Value);
}
FindTokens(child.Value, name, matches);
}
}
else if (containerToken.Type == JTokenType.Array)
{
foreach (JToken child in containerToken.Children())
{
FindTokens(child, name, matches);
}
}
}
}
public string GetParamByKey(string key)
{
var myJson = JsonConvert.SerializeObject(JsonContent);
var objTopas = JObject.Parse(myJson);
var rslt = objTopas.FindTokens(key);
return rslt.ToString();
}