Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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#_List_Json.net_Deserialization - Fatal编程技术网

C# Json转换列表转换错误

C# Json转换列表转换错误,c#,list,json.net,deserialization,C#,List,Json.net,Deserialization,我目前正在开发WinRT应用程序和一个服务中自托管的API,该服务似乎工作得很好 我正在使用json传输数据。我可以序列化我想要传输的类的实例,但是当我想要反序列化时问题就会出现 这节课: public class DtoDirectory { public string Name { get; set; } public string Path { get; set; } public List<string> DirectoryIcon { get; se

我目前正在开发WinRT应用程序和一个服务中自托管的API,该服务似乎工作得很好

我正在使用json传输数据。我可以序列化我想要传输的类的实例,但是当我想要反序列化时问题就会出现

这节课:

public class DtoDirectory
{
    public string Name { get; set; }
    public string Path { get; set; }
    public List<string> DirectoryIcon { get; set; }
}
最后是错误:

{"Could not cast or convert from System.String to System.Collections.Generic.IList`1[DTO.DtoDirectory]."}
Error converting value to type 'System.Collections.Generic.IList`1[DTO.DtoDirectory]'. Path '', line 1, position 1027.
编辑:

调用反序列化程序的代码:

try
        {
            var client = new HttpClient();
            var uri = new Uri("http://localhost:1234/api/document/ListDirectoryRoot?pathDirecory=C:\\Test\\Chasse");
            var request = new HttpRequestMessage(HttpMethod.Get, uri);
            var res = await client.SendAsync(request);
            var content = await res.Content.ReadAsStringAsync();
            var test = SerializeHelper.Deserialize<Wrapper >(lol);
            return content;
        }
        catch (Exception)
        {
            return null;
        }

public class Wrapper
{
  public List<DtoDirectory> list {get;set;}
 }
试试看
{
var client=新的HttpClient();
var uri=新的uri(“http://localhost:1234/api/document/ListDirectoryRoot?pathDirecory=C:\\测试\\Chasse”);
var request=newhttprequestmessage(HttpMethod.Get,uri);
var res=await client.sendaync(请求);
var content=await res.content.ReadAsStringAsync();
var test=SerializeHelper.Deserialize(lol);
返回内容;
}
捕获(例外)
{
返回null;
}
公共类包装器
{
公共列表{get;set;}
}
很不幸,我没有成功地找出错误。。
谢谢你的帮助

不要使用
包装器
,而是使用
列表

var test=SerializeHelper.Deserialize(lol);
在看到您的
包装器
类之后,我假设如果您检查反序列化的异常消息,您将看到以下内容

无法将当前JSON数组(例如[1,2,3])反序列化为类型 “包装器”,因为类型需要JSON对象(例如。 {“name”:“value”})以正确反序列化。要修复此错误 将JSON更改为JSON对象(例如{“name”:“value”})或更改 将类型反序列化为数组或实现集合的类型 接口(如ICollection、IList)类似列表,可以 从JSON数组反序列化。还可以添加JsonArrayAttribute 类型,以强制它从JSON数组反序列化。路径“”, 第1行,位置1


通过简单的异常消息,前4个结果将引导您找到如此相似的问题,这些问题都有解决问题的答案。

我非常确定.NET 4.5中的JSON.NET将正确转换您在问题中指定的值

下面是一个示例,当我反序列化到
列表时,我可以让它正常工作,而不会出现问题:


我在这里怀疑的是,包装器类没有正确反映您从web服务获得的JSON结构。因此,您应该检查确切的JSON结构,并构建一个正确表示对象JSON层次结构的包装器类。

您可以在问题中添加实际使用/调用SerializeHelper方法的代码吗?以及什么是
包装器
?你能不能也把
Wrapper
class贴出来。我做了改动;)尝试删除可能的错误源。制作一个只执行以下操作的示例:创建一个包装器实例,将其序列化为字符串,然后再次对其进行序列化。在任何情况下,反序列化()必须使用与序列化()时相同的类型被调用。@bottus您能检查并告诉我您的代码和fiddle之间有什么区别吗?主要区别来自json中的“\”。。我尝试了你的json(来自jsfield)它可以工作,我尝试了你的json,在路径中添加了一些“\”但它不工作,无论是否使用@@bottus,你能修改它以使其失败吗?我修改了
json
字符串,将
\“
包含在
json
中,它可以工作。我将我的字符串放在您的示例中,它可以工作,但在我的代码中仍然不能工作,我不理解,因为我的代码与您的代码相同。。
{"Could not cast or convert from System.String to System.Collections.Generic.IList`1[DTO.DtoDirectory]."}
Error converting value to type 'System.Collections.Generic.IList`1[DTO.DtoDirectory]'. Path '', line 1, position 1027.
try
        {
            var client = new HttpClient();
            var uri = new Uri("http://localhost:1234/api/document/ListDirectoryRoot?pathDirecory=C:\\Test\\Chasse");
            var request = new HttpRequestMessage(HttpMethod.Get, uri);
            var res = await client.SendAsync(request);
            var content = await res.Content.ReadAsStringAsync();
            var test = SerializeHelper.Deserialize<Wrapper >(lol);
            return content;
        }
        catch (Exception)
        {
            return null;
        }

public class Wrapper
{
  public List<DtoDirectory> list {get;set;}
 }
var test = SerializeHelper.Deserialize<List<DtoDictionary>>(lol);
 var json = "[ { \"Name\": \"ANNONCES LEGALES\", \"Path\": \"C:\\\\Test\\\\ANNONCES LEGALES\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\ANNONCES LEGALES\\\\fond.png\", \"C:\\\\Test\\\\ANNONCES LEGALES\\\\icon.png\" ] }, { \"Name\": \"CONSEIL MUNICIPAL\", \"Path\": \"C:\\\\Test\\\\CONSEIL MUNICIPAL\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\CONSEIL MUNICIPAL\\\\fond.png\", \"C:\\\\Test\\\\CONSEIL MUNICIPAL\\\\icon.png\" ] }, { \"Name\": \"DIVERS\", \"Path\": \"C:\\\\Test\\\\DIVERS\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\DIVERS\\\\fond.png\", \"C:\\\\Test\\\\DIVERS\\\\icon.png\" ] }, { \"Name\": \"ELECTIONS\", \"Path\": \"C:\\\\Test\\\\ELECTIONS\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\ELECTIONS\\\\fond.png\", \"C:\\\\Test\\\\ELECTIONS\\\\icon.png\" ] }, { \"Name\": \"ETAT CIVIL\", \"Path\": \"C:\\\\Test\\\\ETAT CIVIL\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\ETAT CIVIL\\\\fond.png\", \"C:\\\\Test\\\\ETAT CIVIL\\\\icon.png\" ] }, { \"Name\": \"URBANISME\", \"Path\": \"C:\\\\Test\\\\URBANISME\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\URBANISME\\\\fond.png\", \"C:\\\\Test\\\\URBANISME\\\\icon.png\" ] } ]";
 var ret = JsonConvert.DeserializeObject<List<DtoDirectory>>(json);
 var json = "{\"list\":[ { \"Name\": \"ANNONCES LEGALES\", \"Path\": \"C:\\\\Test\\\\ANNONCES LEGALES\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\ANNONCES LEGALES\\\\fond.png\", \"C:\\\\Test\\\\ANNONCES LEGALES\\\\icon.png\" ] }, { \"Name\": \"CONSEIL MUNICIPAL\", \"Path\": \"C:\\\\Test\\\\CONSEIL MUNICIPAL\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\CONSEIL MUNICIPAL\\\\fond.png\", \"C:\\\\Test\\\\CONSEIL MUNICIPAL\\\\icon.png\" ] }, { \"Name\": \"DIVERS\", \"Path\": \"C:\\\\Test\\\\DIVERS\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\DIVERS\\\\fond.png\", \"C:\\\\Test\\\\DIVERS\\\\icon.png\" ] }, { \"Name\": \"ELECTIONS\", \"Path\": \"C:\\\\Test\\\\ELECTIONS\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\ELECTIONS\\\\fond.png\", \"C:\\\\Test\\\\ELECTIONS\\\\icon.png\" ] }, { \"Name\": \"ETAT CIVIL\", \"Path\": \"C:\\\\Test\\\\ETAT CIVIL\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\ETAT CIVIL\\\\fond.png\", \"C:\\\\Test\\\\ETAT CIVIL\\\\icon.png\" ] }, { \"Name\": \"URBANISME\", \"Path\": \"C:\\\\Test\\\\URBANISME\", \"DirectoryIcon\": [ \"C:\\\\Test\\\\URBANISME\\\\fond.png\", \"C:\\\\Test\\\\URBANISME\\\\icon.png\" ] } ]}";
 var ret = JsonConvert.DeserializeObject<Wrapper>(json);