1

After trying to solve it myself and looking for answers on stackoverflow. I need to ask you guys if you can help me.

I dont really know where ir my error, but the fact is that the class I am using to deserialize a json is not being constructed correctly. I get all parameters null.

my class is :

public class Page
{
    public int rolePermission;

    public string icon;

    public string title;

    public string url;

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public Page[] children;

    [JsonConstructor]
    public Page(string rolePermission, string icon, string title, string url, Page[] children)
    {
        this.rolePermission = int.Parse(rolePermission);
        this.icon = icon;
        this.title = title;
        this.url = url;
        this.children = children;
    }
}

The json is:

{
   "page":
   {
      "rolePermission":"2",
      "icon":"dashboard",
      "title":"Dashboard",
      "url":"Dashboard"
   }
}

The children property could be or not on the json and I think there is the problem.

The json with children would be:

 {
       "page":
       {
          "rolePermission":"2",
          "icon":"dashboard",
          "title":"Dashboard",
          "url":"Dashboard",
          "children": 
           {
             "page":{
               "rolePermission":"2",
               "icon":"dashboard",
               "title":"Dashboard",
               "url":"Dashboard"
              }
           }
       }
}

I wish you can help me guys :)

3
  • 2
    I would expect children to be an array in the JSON rather than just an object - and I wouldn't expect a page name, either. Commented Mar 9, 2020 at 13:08
  • @JonSkeet Thank you for the comment Jon. And, do you know why (on the first json without children) is the cosntructor null ? Commented Mar 9, 2020 at 13:12
  • I don't know what you mean by "is the constructor null". (I don't understand the title of the question either.) Commented Mar 9, 2020 at 13:43

1 Answer 1

2

Your JSON is off by one level. You have an extra {} around your root object page, which should be unnamed. Try this:

{
   "rolePermission":"2",
   "icon":"dashboard",
   "title":"Dashboard",
   "url":"Dashboard"
}

Or try this with, as Jon says, an array for children:

   {
     "rolePermission":"2",
     "icon":"dashboard",
     "title":"Dashboard",
     "url":"Dashboard",
     "children": 
     [
       {
         "rolePermission":"2",
         "icon":"dashboard",
         "title":"Dashboard",
         "url":"Dashboard"
       }
     ]
   }
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Kit, I tested it and it works :) Thank you very much for the answer. In agregation I would say that on the Json.Convert we can remove the root item if like in my case we can not change the json structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.