C# JavaScriptSerializer.Deserialize 返回 null
C# JavaScriptSerializer.Deserialize returning null
正在努力反序列化 JSON 字符串,我刚从序列化程序返回 null。
JSON有效,我查过了
我现在盯着这个 for so log 看得眼睛发麻,所以希望比我更有序列化程序经验的人能发现一些东西 'obvious' !?
感谢任何提示:)
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; }
}
请注意,您已将 3 个 bool
属性定义为字符串: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; }
}
请注意,对于下面定义为 object
的属性,您需要一些实际包含它们的 Json 字符串。
编辑:你的代码的第二个问题是,虽然你的示例 json 字符串不是数组,但你正试图反序列化它。尝试更改此行:
jsonResponse = sr.Deserialize<RootObject[]>(jsonString);
对此:
jsonResponse = sr.Deserialize<RootObject>(jsonString);
更改您的数据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; }
}
正在努力反序列化 JSON 字符串,我刚从序列化程序返回 null。
JSON有效,我查过了
我现在盯着这个 for so log 看得眼睛发麻,所以希望比我更有序列化程序经验的人能发现一些东西 'obvious' !?
感谢任何提示:)
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; } }
请注意,您已将 3 个 bool
属性定义为字符串: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; }
}
请注意,对于下面定义为 object
的属性,您需要一些实际包含它们的 Json 字符串。
编辑:你的代码的第二个问题是,虽然你的示例 json 字符串不是数组,但你正试图反序列化它。尝试更改此行:
jsonResponse = sr.Deserialize<RootObject[]>(jsonString);
对此:
jsonResponse = sr.Deserialize<RootObject>(jsonString);
更改您的数据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; }
}