【问题标题】:C# JavaScriptSerializer.Deserialize returning nullC# JavaScriptSerializer.Deserialize 返回 null
【发布时间】:2020-04-21 16:21:08
【问题描述】:

努力反序列化 JSON 字符串,我只是得到 null 从序列化程序返回。

JSON 是有效的,我已经检查过了。

我的眼睛因为盯着这个看,所以现在记录下来,所以希望比我更有序列化器经验的人能发现一些“明显”的东西!?

感谢任何提示:)

RootObject[] jsonResponse = null;
try
{
    if (httpWResp.StatusCode == HttpStatusCode.OK)
    {
        Stream responseStream = httpWResp.GetResponseStream();
        string jsonString = null;
        using (StreamReader reader = new StreamReader(responseStream))
        {
            jsonString = reader.ReadToEnd().Replace("\\", "");
            reader.Close();
        }
        JavaScriptSerializer sr = new JavaScriptSerializer();
        jsonResponse = sr.Deserialize<RootObject[]>(jsonString);

Json 是这样的:

{
    "Tags":[
        {
            "Name":"Requestable",
            "TotalOccurrences":1,
            "SizePercentage":0.33333333333333331
        },
        {"Name":"Generic","TotalOccurrences":1,"SizePercentage":0.33333333333333331},
        {"Name":"YYYYYYY","TotalOccurrences":1,"SizePercentage":0.33333333333333331}
    ],
    "Data":[
        {
            "LogonName":"xxxxxxxxxxxxxx",
            "NetBiosName":"YYYYYYY",
            "FriendlyName":"xxxxxxxx",
            "Description":"xxxxxxxxxx",
            "GroupTypeName":"zzzzzzzzz",
            "AllowJoinRequests":true,
            "RiskFactorTotal":null,
            "IsHighSecurityGroup":false,
            "Email":null,
            "DistinguishedName":"xxx,yyy,xxx",
            "ResourceID":12345,
            "GroupID":6789,
            "ValidUntil":null,
            "IsMailEnabled":false,
            "Notes":null,
            "RiskFactorLastCalculated":null
        }
    ],
    "OutParameters":[
        {"Name":"totalCount","Value":1}
    ]
}

我的课是这样的:

    public class RootObject
    {
        public List<Tag> Tags { get; set; }
        public List<Datum> Data { get; set; }
        public List<OutParameter> OutParameters { get; set; }
    }
    public class Tag
    {
        public string Name { get; set; }
        public int TotalOccurrences { get; set; }
        public double SizePercentage { get; set; }
    }

    public class Datum
    {
        public string LogonName { get; set; }
        public string NetBiosName { get; set; }
        public string FriendlyName { get; set; }
        public string Description { get; set; }
        public string GroupTypeName { get; set; }
        public string AllowJoinRequests { get; set; }
        public string RiskFactorTotal { get; set; }
        public string IsHighSecurityGroup { get; set; }
        public string Email { get; set; }
        public string DistinguishedName { get; set; }
        public int ResourceID { get; set; }
        public int GroupID { get; set; }
        public string ValidUntil { get; set; }
        public string IsMailEnabled { get; set; }
        public string Notes { get; set; }
        public string RiskFactorLastCalculated { get; set; }
    }

    public class OutParameter
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }

【问题讨论】:

    标签: c# json-deserialization javascriptserializer


    【解决方案1】:

    请注意,您已将 3 个 bool 属性定义为字符串:AllowJoinRequestsIsHighSecurityGroupIsMailEnabled

    public partial class RootObject
    {
        [JsonProperty("Tags")]
        public List<Tag> Tags { get; set; }
    
        [JsonProperty("Data")]
        public List<Datum> Data { get; set; }
    
        [JsonProperty("OutParameters")]
        public List<OutParameter> OutParameters { get; set; }
    }
    
    public partial class Datum
    {
        [JsonProperty("LogonName")]
        public string LogonName { get; set; }
    
        [JsonProperty("NetBiosName")]
        public string NetBiosName { get; set; }
    
        [JsonProperty("FriendlyName")]
        public string FriendlyName { get; set; }
    
        [JsonProperty("Description")]
        public string Description { get; set; }
    
        [JsonProperty("GroupTypeName")]
        public string GroupTypeName { get; set; }
    
        [JsonProperty("AllowJoinRequests")]
        public bool AllowJoinRequests { get; set; }
    
        [JsonProperty("RiskFactorTotal")]
        public object RiskFactorTotal { get; set; }
    
        [JsonProperty("IsHighSecurityGroup")]
        public bool IsHighSecurityGroup { get; set; }
    
        [JsonProperty("Email")]
        public object Email { get; set; }
    
        [JsonProperty("DistinguishedName")]
        public string DistinguishedName { get; set; }
    
        [JsonProperty("ResourceID")]
        public long ResourceID { get; set; }
    
        [JsonProperty("GroupID")]
        public long GroupID { get; set; }
    
        [JsonProperty("ValidUntil")]
        public object ValidUntil { get; set; }
    
        [JsonProperty("IsMailEnabled")]
        public bool IsMailEnabled { get; set; }
    
        [JsonProperty("Notes")]
        public object Notes { get; set; }
    
        [JsonProperty("RiskFactorLastCalculated")]
        public object RiskFactorLastCalculated { get; set; }
    }
    
    public partial class OutParameter
    {
        [JsonProperty("Name")]
        public string Name { get; set; }
    
        [JsonProperty("Value")]
        public long Value { get; set; }
    }
    
    public partial class Tag
    {
        [JsonProperty("Name")]
        public string Name { get; set; }
    
        [JsonProperty("TotalOccurrences")]
        public long TotalOccurrences { get; set; }
    
        [JsonProperty("SizePercentage")]
        public double SizePercentage { get; set; }
    }
    

    请注意,对于下面定义为object 的属性,您需要一些实际具有它们的Json 字符串。

    编辑:您的代码的第二个问题是,虽然您的示例 json 字符串不是数组,但您正试图反序列化它。尝试更改此行:

    jsonResponse = sr.Deserialize<RootObject[]>(jsonString);
    

    到这里:

    jsonResponse = sr.Deserialize<RootObject>(jsonString);
    

    【讨论】:

    • 对象类型在原始字符串中为空,因此我将它们作为字符串留在了我的班级中。正如你指出的那样改变了布尔值。不幸的是,它仍然返回 null。想知道是否有任何调试步骤可以缩小它不喜欢的范围?
    • @SimonB 我已经编辑了我的答案以指出您代码中的另一个问题,这应该可以解决您的问题。
    • 感谢新浪。完美的感觉 - 我只是没有看到它!感谢您的帮助:)
    【解决方案2】:

    更改您的基准类

    public class Datum
        {
            public string LogonName { get; set; }
            public string NetBiosName { get; set; }
            public string FriendlyName { get; set; }
            public string Description { get; set; }
            public string GroupTypeName { get; set; }
            public bool AllowJoinRequests { get; set; }
            public string RiskFactorTotal { get; set; }
            public bool IsHighSecurityGroup { get; set; }
            public string Email { get; set; }
            public string DistinguishedName { get; set; }
            public int ResourceID { get; set; }
            public int GroupID { get; set; }
            public string ValidUntil { get; set; }
            public bool IsMailEnabled { get; set; }
            public string Notes { get; set; }
            public string RiskFactorLastCalculated { get; set; }
        }
    

    【讨论】:

    • 谢谢。很好地抓住了布尔类型。不幸的是,它仍然只返回 null。
    猜你喜欢
    • 2023-03-07
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多