Click here to Skip to main content
15,565,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C#
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();
       }
Posted
Updated 10-Apr-22 6:13am

1 solution

The simplest way is to feed your JSON example into a online JSON to C~ converter: Convert JSON to C# Classes Online - Json2CSharp Toolkit[^] is the one I use.
That produces C# classes to match the data:
C#
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class RelanceAutoParCategorie
    {
        public string categorie { get; set; }
        public string statut { get; set; }
        public string @default { get; set; }
    }

    public class RelanceAuto
    {
        public List<RelanceAutoParCategorie> RelanceAutoParCategorie { get; set; }
    }

    public class FeatureRelanceAutoJSON
    {
        public RelanceAuto RelanceAuto { get; set; }
    }

    public class Features
    {
        public bool FeatureActive { get; set; }
        public FeatureRelanceAutoJSON FeatureRelanceAutoJSON { get; set; }
    }

    public class TypeDocument
    {
        public string nomDocument { get; set; }
        public string typeDoc { get; set; }
        public string description { get; set; }
    }

    public class TypeDocumentsJSON
    {
        public List<TypeDocument> TypeDocuments { get; set; }
    }

    public class PortefeuillesJSON
    {
        public List<string> portefeuilles { get; set; }
    }

    public class DataDoc
    {
        [JsonProperty("Doc-Siege")]
        public string DocSiege { get; set; }
        public string Branche { get; set; }
        public TypeDocumentsJSON TypeDocumentsJSON { get; set; }
        public PortefeuillesJSON PortefeuillesJSON { get; set; }
    }

    public class Metadata
    {
        public string name { get; set; }
        public string address { get; set; }
        public string cod { get; set; }
        public string message { get; set; }
    }

    public class Root
    {
        public Features Features { get; set; }

        [JsonProperty("Data-Doc")]
        public DataDoc DataDoc { get; set; }
        public Metadata Metadata { get; set; }
    }
Then use Newtonsoft to read the JSON: Use the Package Manager in VS to load it from NuGet and it's install it to your project.
Then you can use a single line of JSON to process all the data:
C#
Root root = JsonConvert.DeserializeObject<Root>(jsonString);

JsonConvert.DeserializeObject(T) Method (String)[^]
Then you have it as native C# objects and it's pretty simple to do anything you need with it.
 
Share this answer
 
Comments
Member 11573837 10-Apr-22 15:25pm    
Thank you for replaying me, i want to extacte Json noed to put it into string variable because it used in lot of place in application and th json data is more than 2000 line.
I try you solution befor but it's not accepted by my supplier because it used in lot of place like i say :/ so the solution is to extacte node

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900