提问人:Ivan Zlatanov 提问时间:3/26/2020 最后编辑:Ivan Zlatanov 更新时间:3/26/2020 访问量:482
在 JSON 反序列化后处理 null 值
Handle null values after deserialization of JSON
问:
我正在尝试从 [www.wikidata.org][1] 反序列化 JSON 服务器响应,但有时后端不会返回一些预期值,因此每当我调用它们时它们都保持 null。我正在尝试使用此代码处理空值:
public partial class Binding
{
private Label _countryForSport;
private Label _competitionClass;
private Label _citizenshipCountry;
private Label _position;
private Label _team;
private Label _itemLabel;
private Empty _dateOfBirth;
[JsonProperty("_______________")]
public Empty DateOfBirth
{
get
{
return _dateOfBirth;
}
set
{
_dateOfBirth = value;
}
}
[JsonProperty("itemLabel")]
public Label ItemLabel { get; set; }
[JsonProperty("_____________________Label")]
public Label Team
{
get { return _team ?? new Label() { Value = "Unknown", Type = "Unknown", XmlLang = "Unknown" }; }
set { _team = value; }
}
[JsonProperty("_______Label")]
public Label Position
{
get { return _position ?? new Label() { Value = "Unknown", Type = "Unknown", XmlLang = "Unknown" }; }
set { _position = value; }
}
[JsonProperty("competition_classLabel")]
public Label CompetitionClass
{
get { return _competitionClass ?? new Label() { Value = "Unknown", Type = "Unknown", XmlLang = "Unknown" }; }
set { _competitionClass = value; }
}
[JsonProperty("___________Label")]
public Label CitizenshipCountry
{
get { return _citizenshipCountry ?? new Label() { Value = "Unknown", Type = "Unknown", XmlLang = "Unknown" }; }
set { _citizenshipCountry = value; }
}
[JsonProperty("country_for_sportLabel")]
public Label CountryForSport
{
get { return _countryForSport ?? new Label() { Value = "Unknown", Type = "Unknown", XmlLang = "Unknown" }; }
set { _countryForSport = value; }
}
}
这是标签:
public partial class Label
{
[JsonProperty("xml:lang")]
public string XmlLang { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("value")]
public string Value
{
get; set;
}
}
问题不在于它仍然返回 null,而在于它总是返回“Unknown”,无论服务器是否返回字段并且我反序列化它们。
这是整个代码(包括解串器):
partial class ServerResponse
{
[JsonProperty("head")]
public Head Head { get; set; }
[JsonProperty("results")]
public Results Results { get; set; }
}
public partial class Head
{
[JsonProperty("vars")]
public string[] Vars { get; set; }
}
public partial class Results
{
[JsonProperty("bindings")]
public Binding[] Bindings { get; set; }
}
public partial class Binding
{
private Label _countryForSport;
private Label _competitionClass;
private Label _citizenshipCountry;
private Label _position;
private Label _team;
private Label _itemLabel;
private Empty _dateOfBirth;
[JsonProperty("_______________")]
public Empty DateOfBirth
{
get
{
return _dateOfBirth;
}
set
{
_dateOfBirth = value;
}
}
[JsonProperty("itemLabel")]
public Label ItemLabel { get; set; }
[JsonProperty("_____________________Label")]
public Label Team
{
get { return _team ?? new Label() { Value = "Unknown", Type = "Unknown", XmlLang = "Unknown" }; }
set { _team = value; }
}
[JsonProperty("_______Label")]
public Label Position
{
get { return _position ?? new Label() { Value = "Unknown", Type = "Unknown", XmlLang = "Unknown" }; }
set { _position = value; }
}
[JsonProperty("competition_classLabel")]
public Label CompetitionClass
{
get { return _competitionClass ?? new Label() { Value = "Unknown", Type = "Unknown", XmlLang = "Unknown" }; }
set { _competitionClass = value; }
}
[JsonProperty("___________Label")]
public Label CitizenshipCountry
{
get { return _citizenshipCountry ?? new Label() { Value = "Unknown", Type = "Unknown", XmlLang = "Unknown" }; }
set { _citizenshipCountry = value; }
}
[JsonProperty("country_for_sportLabel")]
public Label CountryForSport
{
get { return _countryForSport ?? new Label() { Value = "Unknown", Type = "Unknown", XmlLang = "Unknown" }; }
set { _countryForSport = value; }
}
}
public partial class Label
{
[JsonProperty("xml:lang")]
public string XmlLang { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("value")]
public string Value
{
get; set;
}
}
public partial class Empty
{
[JsonProperty("datatype")]
public Uri Datatype { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("value")]
public DateTimeOffset Value { get; set; }
}
public partial class ServerResponse
{
public static ServerResponse FromJson(string json) => JsonConvert.DeserializeObject<ServerResponse>(json, JsonHandler.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this ServerResponse self) => JsonConvert.SerializeObject(self, JsonHandler.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
关于如何正确处理 null 并在服务器返回正确响应时仍能获得值的任何想法?
提前致谢!
编辑:
JSON 字符串示例:
"head" : {
"vars" : [ "itemLabel", "_____________________Label", "_______Label", "competition_classLabel", "_______________", "___________Label", "country_for_sportLabel" ]
},
"results" : {
"bindings" : [ {
"_______________" : {
"datatype" : "http://www.w3.org/2001/XMLSchema#dateTime",
"type" : "literal",
"value" : "1980-09-12T00:00:00Z"
},
"itemLabel" : {
"xml:lang" : "en",
"type" : "literal",
"value" : "Yao Ming"
},
"_____________________Label" : {
"xml:lang" : "en",
"type" : "literal",
"value" : "Houston Rockets"
},
"_______Label" : {
"xml:lang" : "en",
"type" : "literal",
"value" : "center"
},
"competition_classLabel" : {
"xml:lang" : "en",
"type" : "literal",
"value" : "men's basketball"
},
"___________Label" : {
"xml:lang" : "en",
"type" : "literal",
"value" : "People's Republic of China"
},
"country_for_sportLabel" : {
"xml:lang" : "en",
"type" : "literal",
"value" : "United States of America"
}
} ]
}
}
[1]: https://www.wikidata.org/
答: 暂无答案
评论
ObjectCreationHandling = ObjectCreationHandling.Replace
JsonSerializerSettings