Я пытаюсь десериализовать JSON ответ сервера от [www.wikidata.org] [1], но иногда бэкэнд не возвращает некоторые ожидаемые значения, поэтому они остаются нулевыми, когда я их вызываю. Я пытаюсь обработать нулевые значения с помощью этого кода:
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;
}
}
Проблема не в том, что он по-прежнему возвращает ноль, а в том, что он всегда возвращает «Неизвестно», независимо от того, возвращает ли сервер поля, и я десериализую их.
Это весь код (включая десериализатор):
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 }
},
};
}
Любые идеи о том, как я могу правильно обрабатывать нуль и все равно получать значения всякий раз, когда сервер возвращает правильный ответ?
Заранее спасибо!
РЕДАКТИРОВАТЬ:
Пример строки 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/