locked
How do I deserialize this JSON file? RRS feed

  • Question

  • User-78754308 posted

    Hi all,

    Please forgive the newbie question, but I'm having trouble deserializing a JSON collection and I all the examples I've found don't seem to work for my scenario.  

    When attempting to deserialize this file, I only get NULLs in return.  Here's the file:

    {
      "elements": [
        {
          "name": "Elements",
          "items": [
            {
              "name": "input text",
              "icon": "fa fa-text-width",
              "content": "elements/input-text.html"
            },
            {
              "name": "input date",
              "icon": "fa fa-calendar",
              "content": "elements/input-date.html"
            },
            {
              "name": "textarea",
              "icon": "fa fa-pencil-square-o",
              "content": "elements/input-textarea.html"
            },
            {
              "name": "input number",
              "icon": "fa fa-sort-numeric-asc",
              "content": "elements/input-number.html"
            },
            {
              "name": " input email",
              "icon": "fa fa-envelope ",
              "content": "elements/input-email.html"
            },
            {
              "name": "file",
              "icon": "fa fa-cloud-upload",
              "content": "elements/input-file.html"
            },
            {
              "name": "checkbox",
              "icon": "fa fa-check ",
              "content": "elements/input-checkbox.html"
            },
            {
              "name": "select",
              "icon": "fa fa-chevron-circle-down ",
              "content": "elements/input-select.html"
            },
            {
              "name": "radio",
              "icon": "fa fa-check-circle ",
              "content": "elements/input-radio.html"
            },
            {
              "name": "Heading",
              "icon": "fa fa-header ",
              "content": "elements/heading.html"
            },
            {
              "name": "paragraph",
              "icon": "fa fa-paragraph",
              "content": "elements/paragraph.html"
            },
            {
              "name": "Divider",
              "icon": "fa fa-minus ",
              "content": "elements/divider.html"
            }
          ]
        }
      ]
    
    }
    

    And here's my code:

    List<Element> jsonElements = new List<Element>();            
    
    //Get the list of elements
    using (StreamReader file = File.OpenText(Server.MapPath(elementsDictionary)))
         {
              JsonSerializer serializer = new JsonSerializer();
              jsonElements.Add((Element)serializer.Deserialize(file, typeof(Element)));
         }
    
    //Loop through each element, build the menu selector for it
    foreach (var element in jsonElements)
         {
              //Do stuff here
         }
    
    public class Element
         {
             public string name { get; set; }
             public string icon { get; set; }
             public string content { get; set; }
         }

    Could anyone tell me what obvious dumb thing I'm doing wrong?  I'd greatly greatly appreciate it.

    Thanks in advance!

    Friday, August 16, 2019 4:12 PM

All replies

  • User475983607 posted

    The  C# class does not match the JSON format.  Use Visual Studio to create classes.  Copy the JSON, then in Visual Studio click Edit -> Paste Special -> Paste JSON as Classes.

        public class Element
        {
            public string name { get; set; }
            public Item[] items { get; set; }
        }
    
        public class Item
        {
            public string name { get; set; }
            public string icon { get; set; }
            public string content { get; set; }
        }

    You're deserializing a collection.  

    Element[] elements

    Or 

    List<Element> elements

    https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

    Saturday, August 17, 2019 11:17 AM
  • User665608656 posted

    Hi Piornet.

    According to your description, I found that your json text file contains hierarchical relationships, and that's why you didn't get value in jsonElements.

    In this case, you need to use the dynamic type to accept your deserialized data.

    Here is the code:

              List<Element> jsonElements = new List<Element>();
    
                    //Get the list of elements
                    using (StreamReader file = File.OpenText(Server.MapPath(elementsDictionary)))
                    {
                        dynamic stuff = JsonConvert.DeserializeObject(File.ReadAllText(Server.MapPath(elementsDictionary)));
                        for (int i = 0; i < stuff.elements[0].items.Count; i++)
                        {
                            Element element = new Element();
                            element.name = stuff.elements[0].items[i].name;
                            element.icon = stuff.elements[0].items[i].icon;
                            element.content = stuff.elements[0].items[i].content;
                            jsonElements.Add(element);
                        }
                    }

    You can also refer to this link : Deserialize JSON into C# dynamic object?

    Best Regards,

    YongQing.

    Monday, August 19, 2019 3:32 AM