0

Struggling to deserialize a JSON string, i am just getting null returned form the serializer.

JSON is valid, I have checked.

My eyes are going boggledy-boop from staring at this for so log now, so hoping that someone with more experience of the serializer than me, could spot something 'obvious' !?

Thanks for any tips :)

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 is thus :

{
    "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}
    ]
}

And my classes are thus :

    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; }
    }

2 Answers 2

1

Please note that there are 3 bool properties which you have defined as string: AllowJoinRequests, IsHighSecurityGroup, IsMailEnabled:

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; }
}

Please note that for properties that are defined as object below, you need some Json string that actually have them.

EDIT: Second problem with your code is that while your sample json string is not an array, you are trying to deserialize it as such. try changing this line:

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

to this:

jsonResponse = sr.Deserialize<RootObject>(jsonString);
Sign up to request clarification or add additional context in comments.

3 Comments

The object types are null in the original string and so I have left these as string in my class. Changed the bool as you pointed out. Unfortunately it still returns null. Wondering if there is any debugging steps tat could narrow down on what it doesn't like ?
@SimonB I have edited my answer to point out another problem in your code, that should solve your problem.
Thanks Sina. Perfect sense - I just wasn't seeing it !! Your help appreciated :)
1

Change your Datum class

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; }
    }

1 Comment

Thanks. Good catch with the bool types. Unfortunately it still returns just null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.