none
form keys from a JSON file RRS feed

  • Question

  • Hey friends

    I need your hepl about state machine, I have a file JSON and  I have to form keys

    example:

    {
                    "PERSONNE":{
                        "NOM": "Marc",
                        "PRENOM":"Remy";
                    }
                    "ADRESSE":{
                        "MAIL":"aaa@gmail.com";
                        "CODEPOSTAL": "11111";
                    }
    }

    the keys you get should look like this:

    PERSONNE.NOM

    PERSONNE.PRENOM

    ADRESSE.MAIL

    ADRESSE.CODEPOSTAL

    I've tried a lot of things, but nothing so far.

    thanks for your help

    Thursday, March 12, 2020 7:59 AM

All replies

  • //Add reference System.IO;
    // Add a reference to System.Web.Extensions.dll.
    		string serialization = File.ReadAllText(@"c:\videogames.json");
    
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Dictionary<string, object> objects =
            (Dictionary<string, object>)serializer.DeserializeObject(serialization);
    
       forEach(var o in objects)
       {
    	string n = o.PERSONNE.NOM;
    
        }

    Thursday, March 12, 2020 12:59 PM
  • Hello,

    An easy way is shown below where I have modified your json as it was malformed.

    {
      "PERSONNE": {
        "NOM": "Marc",
        "PRENOM": "Remy"
      },
      "ADRESSE": {
        "MAIL": "aaa@gmail.com",
        "CODEPOSTAL": "11111"
      }
    }

    Requires Json.net from NuGet

    https://www.nuget.org/packages/Newtonsoft.Json/

    Classes

    public class Rootobject
    {
        public PERSONNE PERSONNE { get; set; }
        public ADRESSE ADRESSE { get; set; }
    }
    
    public class PERSONNE
    {
        public string NOM { get; set; }
        public string PRENOM { get; set; }
    }
    
    public class ADRESSE
    {
        public string MAIL { get; set; }
        public string CODEPOSTAL { get; set; }
    }
    

    Form code

    private void button1_Click(object sender, EventArgs e)
    {
        var json = File.ReadAllText("File.json");
        var result = JsonConvert.DeserializeObject<Rootobject>(json);
    
        Console.WriteLine(result.PERSONNE.NOM);
        Console.WriteLine(result.PERSONNE.PRENOM);
        Console.WriteLine(result.ADRESSE.MAIL);
        Console.WriteLine(result.ADRESSE.CODEPOSTAL);
    
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, March 12, 2020 1:31 PM
    Moderator
  • Hi little geek,

    Thank you for posting here.

    If there are many properties in the json, or you don't know the specific properties, you can also use the following way to dynamically obtain all properties.

            static void Main(string[] args)
            {
                string json = @"{'PERSONNE':{'NOM':'Marc','PRENOM':'Remy'},'ADRESSE':{'MAIL':'aaa @gmail.com','CODEPOSTAL':'11111'}}";
                dynamic dynObj = JsonConvert.DeserializeObject(json);
    
                var jObj = (JObject)dynObj;
    
                foreach (var member in GetMembers(jObj))
                {
                    Console.WriteLine(member);
                }
                Console.ReadKey();
            }
            private static IEnumerable<string> GetMembers(JToken jToken)
            {
                var members = new List<string>();
    
                if (jToken is JObject)
                {
                    var jObject = (JObject)jToken;
    
                    foreach (var prop in jObject.Properties())
                    {
                        if (prop.Value is JValue)
                        {
                            members.Add(prop.Name);
                        }
                        else
                        {
                            members.AddRange(GetMembers(prop.Value).Select(member => prop.Name + "." + member));
                        }
                    }
                }
                else if (jToken is JArray)
                {
                    var jArray = (JArray)jToken;
    
                    for (var i = 0; i < jArray.Count; i++)
                    {
                        var token = jArray[i];
    
                        members.AddRange(GetMembers(token).Select(member => i + "." + member));
                    }
                }
                return members;
            }

    Hope this coule be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 13, 2020 3:20 AM