Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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_Deserialization - Fatal编程技术网

C# 无法将Json对象反序列化为类型

C# 无法将Json对象反序列化为类型,c#,json,json.net,deserialization,C#,Json,Json.net,Deserialization,我有一个下面的Json对象,我试图将Json对象反序列化为“type”,但它显示了一条错误消息 无法反序列化当前JSON对象(例如{“名称”:“值”}) 输入类型“System.Collections.Generic.List`1[khdatasync.response]” 因为该类型需要一个JSON数组(例如[1,2,3])来反序列化 正确地要修复此错误,请将JSON更改为JSON数组 (例如[1,2,3])或更改反序列化类型,使其成为正常类型 .NET类型(例如,不是integer之类的基元

我有一个下面的Json对象,我试图将Json对象反序列化为“type”,但它显示了一条错误消息

无法反序列化当前JSON对象(例如{“名称”:“值”}) 输入类型“System.Collections.Generic.List`1[khdatasync.response]” 因为该类型需要一个JSON数组(例如[1,2,3])来反序列化 正确地要修复此错误,请将JSON更改为JSON数组 (例如[1,2,3])或更改反序列化类型,使其成为正常类型 .NET类型(例如,不是integer之类的基元类型,也不是集合 可以从JSON反序列化的类型(如数组或列表) 对象还可以将JsonObjectAttribute添加到类型以强制它 从JSON对象反序列化。路径“响应”,第2行,位置 十四,

请检查我下面的代码,并建议如何解决此问题

Json数据:

{
  "response": 
    {
      "uri": "\/api\/junaid@khindia.com",
      "action": "MYWORKSPACELIST",
      "result": [{"workspaceId":"2102454000005681734","workspaceName":"Zoho Desk Analytics (Sample)-Copy","workspaceDesc":"Zoho Desk Analytics reporting database providing insightful reports and dashboards on the Help desk data collected.","orgId":"698551553","createdTime":"1596886142038","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000005398071","workspaceName":"MyData","workspaceDesc":"","orgId":"698551553","createdTime":"1596095878304","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000005201413","workspaceName":"GloveOrder","workspaceDesc":"","orgId":"698551553","createdTime":"1595582160570","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000003663782","workspaceName":"TAD","workspaceDesc":"","orgId":"698551553","createdTime":"1591781832434","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000003605784","workspaceName":"Glove Factory","workspaceDesc":"This workspace for factory people to develop Reports and Dashboards","orgId":"698551553","createdTime":"1591618269812","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000001881026","workspaceName":"FWD","workspaceDesc":"This is for Shoes Division","orgId":"698551553","createdTime":"1583817905690","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000000664009","workspaceName":"HO Details","workspaceDesc":"","orgId":"698551553","createdTime":"1580642044086","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000000323397","workspaceName":"KH Retail","workspaceDesc":"","orgId":"698551553","createdTime":"1578720947711","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000000319814","workspaceName":"H & S","workspaceDesc":"","orgId":"698551553","createdTime":"1578664170637","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000000245988","workspaceName":"Teamwork Projects - Sample","workspaceDesc":"","orgId":"698551553","createdTime":"1578224003870","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000000245069","workspaceName":"Facebook Page competitive Analytics (Sample)","workspaceDesc":"Facebook Competitive Analytics workspace providing insightful comparison reports and dashboards from the data collected from your competitors.","orgId":"698551553","createdTime":"1578223755327","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000000239193","workspaceName":"Shopify Analytics - Sample","workspaceDesc":"Shopify Analytics workspace providing insightful reports and dashboards on the data collected.","orgId":"698551553","createdTime":"1578223215548","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000000228568","workspaceName":"Zoho Finance Advanced Analytics  (Sample)","workspaceDesc":"Zoho Finance Analytics reporting database providing insightful reports and dashboards on the data collected.","orgId":"698551553","createdTime":"1578223092773","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000000227303","workspaceName":"Super Store Sales","workspaceDesc":"Super Store Sales database with sales data for the last 5 years","orgId":"698551553","createdTime":"1578222583948","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000000216894","workspaceName":"Zoho People - Sample-Copy","workspaceDesc":"","orgId":"698551553","createdTime":"1578221159551","createdBy":"junaid@khindia.com","isDefault":false},{"workspaceId":"2102454000000044365","workspaceName":"Gloves","workspaceDesc":"This is for Gloves Data","orgId":"698551553","createdTime":"1576594180567","createdBy":"junaid@khindia.com","isDefault":true},{"workspaceId":"2102454000000004001","workspaceName":"LGD","workspaceDesc":"","orgId":"698551553","createdTime":"1576246723570","createdBy":"junaid@khindia.com","isDefault":false}]
  }
}
C#代码:

公共类结果
{
公共字符串workspaceId{get;set;}
公共字符串工作空间名称{get;set;}
公共字符串workspaceDesc{get;set;}
公共字符串orgId{get;set;}
公共字符串createdTime{get;set;}
通过{get;set;}创建的公共字符串
公共布尔值是默认值{get;set;}
}
公众课堂反应
{
公共字符串uri{get;set;}
公共字符串操作{get;set;}
公共列表结果{get;set;}
}
public void GetWorkSpaceList(IReportClient rc)
{
尝试
{                
字符串uri=rc.GetURI(电子邮件);
string result=rc.MyWorkspaceList(uri,null);
**List res=JsonConvert.DeserializeObject(结果)**
}
捕获(例外情况除外)
{
XtraMessageBox.Show(例如Message);
}
}
有几个问题:

  • 您的外部JSON结构是一个对象,而不是一个列表——这是错误消息告诉您的。它无法将非列表的内容转换为C#列表结构

  • 您错误地使用外部类来表示外部JSON对象—您所得到的只是结果和响应部分—但它们都是外部包装器中的结构

  • 您需要此类结构:

       public class Result    {
         public string workspaceId { get; set; } 
         public string workspaceName { get; set; } 
         public string workspaceDesc { get; set; } 
         public string orgId { get; set; } 
         public string createdTime { get; set; } 
         public string createdBy { get; set; } 
         public bool isDefault { get; set; } 
       }
    
       public class Response    {
         public string uri { get; set; } 
         public string action { get; set; } 
         public List<Result> result { get; set; } 
      }
    
      public class Root    {
         public Response response { get; set; } 
      }
    
    公共类结果{
    公共字符串workspaceId{get;set;}
    公共字符串工作空间名称{get;set;}
    公共字符串workspaceDesc{get;set;}
    公共字符串orgId{get;set;}
    公共字符串createdTime{get;set;}
    通过{get;set;}创建的公共字符串
    公共布尔值是默认值{get;set;}
    }
    公众课堂反应{
    公共字符串uri{get;set;}
    公共字符串操作{get;set;}
    公共列表结果{get;set;}
    }
    公共类根{
    公共响应{get;set;}
    }
    
    (通过将JSON粘贴到中自动生成-这是一个非常方便的工具。)

    然后你就可以写了

    Root data = JsonConvert.DeserializeObject<Root>(result);
    //and to get a specific piece of data, you could write, for example:
    var action = data.response.action;
    
    Root data=JsonConvert.DeserializeObject(结果);
    //要获取特定的数据,您可以编写,例如:
    var action=data.response.action;
    
    您的数据是单个对象的表示,而不是列表/数组。将其反序列化为
    响应
    ,而不是
    列表
    感谢您的回复…我尝试了上述代码,但返回了值null@Cid表示:
    response res=JsonConvert.DeserializeObject(result)顺便说一句,您的类和方法名称很混乱。您说了
    GetWorkSpaceList
    ,但什么也没有返回
    response
    对象的名称以小写字母开头,还包括名为
    result
    的属性。它的类型也是
    List
    Root data = JsonConvert.DeserializeObject<Root>(result);
    //and to get a specific piece of data, you could write, for example:
    var action = data.response.action;