C# 反序列化JSON后处理空值

C# 反序列化JSON后处理空值,c#,json,serialization,nullpointerexception,json.net,C#,Json,Serialization,Nullpointerexception,Json.net,我试图从[www.wikidata.org][1]反序列化JSON服务器响应,但有时后端不会返回一些预期值,因此每当我调用它们时,它们都保持为null。我试图用以下代码处理空值: public partial class Binding { private Label _countryForSport; private Label _competitionClass; private Label _citizenshipCountry; private Labe

我试图从[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”,不管服务器是否返回字段并对其进行反序列化

这是全部代码(包括反序列化程序):


你能发布一个JSON被反序列化的例子吗?我添加了它。抱歉忘了这件事!标签丢失时会出现空值(例如“competition_classLabel”)。您可以通过将
ObjectCreationHandling=ObjectCreationHandling.Replace
添加到
JsonSerializerSettings
来解决此问题。请参阅,以获得有关出错原因的完整解释。您能否发布一个JSON被反序列化的示例?我添加了它。抱歉忘了这件事!标签丢失时会出现空值(例如“competition_classLabel”)。您可以通过将
ObjectCreationHandling=ObjectCreationHandling.Replace
添加到
JsonSerializerSettings
来解决此问题。请参阅,以获取有关出错原因的完整解释。
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 }
        },
    };
}
  "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/