C# 反序列化json格式字符串

C# 反序列化json格式字符串,c#,string,websocket,deserialization,C#,String,Websocket,Deserialization,目前,我有一个websocket应用程序,它从服务器接收响应 我正在使用 object obj=new JavaScriptSerializer().Deserialize<CustomerNTF>(e.data); 我所尝试的: public class CustomerNTF { public face_list face_list { get; set; } public int face_num{get;set;} public string msg

目前,我有一个websocket应用程序,它从服务器接收响应

我正在使用

object obj=new JavaScriptSerializer().Deserialize<CustomerNTF>(e.data);
我所尝试的:

public class CustomerNTF
{
    public face_list face_list { get; set; }
    public int face_num{get;set;}
    public string msg_id{get;set;}
}
public class face_list
{
    public class face_detect
    {
        public int age { get; set; }
        public int beauty { get; set; }
        public int expression { get; set; }
        public int gender { get; set; }
        public bool glass { get; set; }
        public int smile { get; set; }
    }
    public class face_recg
    {
        public int confidence { get; set; }
        public string name { get; set; }
        public string person_id { get; set; }
    }
}

我收到的错误是数组的反序列化不支持类型face_列表。

您的类结构错误。json字符串包含面列表,而不是单个对象。此外,您当前的face_list类不包含任何属性,只有两个嵌套的类定义不包含任何值

正确的结构:

public class FaceDetect
{
    public int age { get; set; }
    public int beauty { get; set; }
    public int expression { get; set; }
    public int gender { get; set; }
    public bool glass { get; set; }
    public int smile { get; set; }
}

public class FaceRecg
{
    public int confidence { get; set; }
    public string name { get; set; }
    public string person_id { get; set; }
}

public class FaceList
{
    public FaceDetect face_detect { get; set; }
    public FaceRecg face_recg { get; set; }
}

public class CustomerNTF
{
    public List<FaceList> face_list { get; set; }
    public int face_num { get; set; }
    public string msg_id { get; set; }
}

将来,如果您不确定类结构与给定json字符串的匹配情况,可以使用类似的工具。这为您生成了正确的结构。

不要发布您对错误的解释,阅读并研究实际错误。公共face_list face_list应该是一个集合,而不是一个单独的项,而且您的类还有更多错误。您可以根据JSON结构在Visual Studio中通过编辑->粘贴特殊->粘贴JSON作为类,或在上生成适当的类。@CodeCaster不知道有JSON字符串转换器,谢谢!啊,不知道工具json2csharp,昨天刚听说了术语反序列化,所以我想我会试试,谢谢你的建议!
public class FaceDetect
{
    public int age { get; set; }
    public int beauty { get; set; }
    public int expression { get; set; }
    public int gender { get; set; }
    public bool glass { get; set; }
    public int smile { get; set; }
}

public class FaceRecg
{
    public int confidence { get; set; }
    public string name { get; set; }
    public string person_id { get; set; }
}

public class FaceList
{
    public FaceDetect face_detect { get; set; }
    public FaceRecg face_recg { get; set; }
}

public class CustomerNTF
{
    public List<FaceList> face_list { get; set; }
    public int face_num { get; set; }
    public string msg_id { get; set; }
}