Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有空参数的Json构造函数_C#_.net_Json_Json.net - Fatal编程技术网

C# 带有空参数的Json构造函数

C# 带有空参数的Json构造函数,c#,.net,json,json.net,C#,.net,Json,Json.net,在我自己尝试解决并寻找stackoverflow的答案之后。我想问问你们是否能帮我 我真的不知道我的错误在哪里,但事实是我用来反序列化json的类没有被正确构造。我得到的所有参数都为空 我的班级是: public class Page { public int rolePermission; public string icon; public string title; public string url; [JsonProperty(NullV

在我自己尝试解决并寻找stackoverflow的答案之后。我想问问你们是否能帮我

我真的不知道我的错误在哪里,但事实是我用来反序列化json的类没有被正确构造。我得到的所有参数都为空

我的班级是:

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;
    }
}
json是:

{
   "page":
   {
      "rolePermission":"2",
      "icon":"dashboard",
      "title":"Dashboard",
      "url":"Dashboard"
   }
}
children属性可能在json上,也可能不在json上,我认为这就是问题所在

带子对象的json将是:

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

我希望你们能帮助我,伙计们:)

你们的JSON被关闭了一级。在根对象
页面
周围有一个额外的
{}
,应该没有命名。试试这个:

{
   "rolePermission":"2",
   "icon":"dashboard",
   "title":"Dashboard",
   "url":"Dashboard"
}
或者像Jon说的那样,用一个用于
子对象的数组来试试这个:

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

我希望
children
是JSON中的一个数组,而不仅仅是一个对象-我也不希望
页面
名称。@jonsket谢谢你的评论Jon。而且,你知道为什么(在第一个没有子项的json上)协构造函数为null吗?我不知道你所说的“构造函数为null”是什么意思。(我也不明白问题的标题。)嗨,Kit,我测试了它,它很有效:)非常感谢你的回答。在agregation中,我会说在Json.Convert上,如果愿意,我们可以删除根项。在我的例子中,我们不能更改Json结构。