Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.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#_Json_Json.net_Restsharp_Json Deserialization - Fatal编程技术网

C# 如何解析多级JSON数组

C# 如何解析多级JSON数组,c#,json,json.net,restsharp,json-deserialization,C#,Json,Json.net,Restsharp,Json Deserialization,我试图解析多级json数组,但遇到以下异常: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Tractor.Models.UserDetails' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. 以下是json响应: { "status": 1, "message":

我试图解析多级json数组,但遇到以下异常:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Tractor.Models.UserDetails' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
以下是json响应:

{
  "status": 1,
  "message": null,
  "userdetails": [
    {
      "ID": 9,
      "Name": "aleem",
      "Company_Name": null,
      "Email": null,
      "Password": null,
      "Created_Date": null,
      "Email_Confirm": null,
      "Token": null,
      "Phone": null,
      "Website": null,
      "allcompanies": [
        {
          "Name": "self",
          "allprojects": [
            {
              "ID": 1,
              "Name": "test"
            }
          ]
        }
      ],
      "screens": 3
    }
  ]
}
下面是我为获得JSON响应而添加的类:

class LoginApiResponse
{
        [JsonProperty("status")]
        public int Status { get; set; }

        [JsonProperty("message")]
        public string Message { get; set; }

        [JsonProperty("userdetails")]
        public UserDetails UserDetails { get; set; }

    }

class UserDetails
{
    [JsonProperty("ID")]
    public int ID { get; set; }

    [JsonProperty("Name")]
    public string Name { get; set; }

    [JsonProperty("Company_Name")]
    public string Company_Name { get; set; }

    [JsonProperty("Email")]
    public string Email { get; set; }

    [JsonProperty("Password")]
    public string Password { get; set; }

    [JsonProperty("Created_Date")]
    public string Created_Date { get; set; }

    [JsonProperty("Email_Confirm")]
    public string Email_Confirm { get; set; }

    [JsonProperty("Token")]
    public string Token { get; set; }

    [JsonProperty("Phone")]
    public string Phone { get; set; }

    [JsonProperty("Website")]
    public string Website { get; set; }

    [JsonProperty("allcompanies")]
    public List<Company> Companies { get; set; }

    [JsonProperty("screens")]
    public int Screens { get; set; }
}

class Company
    {
        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("allprojects")]
        public List<Project> Projects {get;set;}
    }

class Project
    {
        [JsonProperty("ID")]
        public int ID { get; set; }

        [JsonProperty("Name")]
        public string Name { get; set; }
    }
class LoginApiResponse
{
[JsonProperty(“状态”)]
公共int状态{get;set;}
[JsonProperty(“消息”)]
公共字符串消息{get;set;}
[JsonProperty(“用户详细信息”)]
public UserDetails UserDetails{get;set;}
}
类用户详细信息
{
[JsonProperty(“ID”)]
公共int ID{get;set;}
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
[JsonProperty(“公司名称”)]
公共字符串公司名称{get;set;}
[JsonProperty(“电子邮件”)]
公共字符串电子邮件{get;set;}
[JsonProperty(“密码”)]
公共字符串密码{get;set;}
[JsonProperty(“创建日期”)]
创建的公共字符串\u日期{get;set;}
[JsonProperty(“电子邮件确认”)]
公共字符串电子邮件\u确认{get;set;}
[JsonProperty(“令牌”)]
公共字符串标记{get;set;}
[JsonProperty(“电话”)]
公用字符串电话{get;set;}
[JsonProperty(“网站”)]
公共字符串网站{get;set;}
[JsonProperty(“所有公司”)]
上市公司{get;set;}
[JsonProperty(“屏幕”)]
公共整数屏幕{get;set;}
}
阶级公司
{
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
[JsonProperty(“所有项目”)]
公共列表项目{get;set;}
}
班级项目
{
[JsonProperty(“ID”)]
公共int ID{get;set;}
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
}
我正在使用以下代码序列化此JSON:

var response = client.Execute(request); 
var final = JsonConvert.DeserializeObject<LoginApiResponse>(response.Content);
var response=client.Execute(请求);
var final=JsonConvert.DeserializeObject(response.Content);

JSON要求
UserDetails
字段是
UserDetails
对象的数组(或列表):

class LoginApiResponse
{
    //.........
    [JsonProperty("userdetails")]
    public UserDetails[] UserDetails { get; set; }
    //                ^^
    // Alternatively, use List<UserDetails> instead of UserDetails[]
}
class LoginApiResponse
{
//.........
[JsonProperty(“用户详细信息”)]
public UserDetails[]UserDetails{get;set;}
//                ^^
//或者,使用List而不是UserDetails[]
}

JSON要求
UserDetails
字段是
UserDetails
对象的数组(或列表):

class LoginApiResponse
{
    //.........
    [JsonProperty("userdetails")]
    public UserDetails[] UserDetails { get; set; }
    //                ^^
    // Alternatively, use List<UserDetails> instead of UserDetails[]
}
class LoginApiResponse
{
//.........
[JsonProperty(“用户详细信息”)]
public UserDetails[]UserDetails{get;set;}
//                ^^
//或者,使用List而不是UserDetails[]
}

我这样做的方式是将JSON对象反序列化为动态变量,然后访问该动态变量所需的属性,如果愿意,可以使用这些属性初始化模型。

我这样做的方式是将JSON对象反序列化为动态变量,然后访问该动态变量所需的属性,如果愿意,您可以使用它们来初始化模型。

您的问题在
LoginApiResponse
类中

属性
UserDetails
应该是
列表,而不是
UserDetails
。所以你得到:

class LoginApiResponse
{
    [JsonProperty("status")]
    public int Status { get; set; }

    [JsonProperty("message")]
    public string Message { get; set; }

    [JsonProperty("userdetails")]
    public List<UserDetails> UserDetails { get; set; }

}
或者(如果您不希望看到用户详细信息列表),您也可以更改json(从列表更改为对象):


您的问题在
LoginApiResponse
类中

属性
UserDetails
应该是
列表,而不是
UserDetails
。所以你得到:

class LoginApiResponse
{
    [JsonProperty("status")]
    public int Status { get; set; }

    [JsonProperty("message")]
    public string Message { get; set; }

    [JsonProperty("userdetails")]
    public List<UserDetails> UserDetails { get; set; }

}
或者(如果您不希望看到用户详细信息列表),您也可以更改json(从列表更改为对象):


UserDetails
是Json中的数组
UserDetails
是Json中的数组