rád by som Json.NET
hádzať JsonSerializationException
keď Json
reťazca chýba vlastnosť, ktorú vyžaduje trieda C #.
K dispozícii je MissingMemberHandling Enumeration
ktorý
Ak je chýbajúcim členom, hodí JsonSerializationException počas deserializácie.
ale myslím, že to je opačné miesto toho, čo chcem. Myslím, že to znamená chýbajúci člen na triede c #. Chcem chýbajúceho člena Jsona.
Môj kód je
public MyObj Deserialise(string json)
{
var jsonSettings = new JsonSerializerSettings();
jsonSettings.MissingMemberHandling = MissingMemberHandling.Error;
return JsonConvert.DeserializeObject<ApiMessage>(json, jsonSettings);
}
Napríklad
public class MyObj
{
public string P1 { get; set; }
public string P2 { get; set; }
}
string json = @"{ ""P1"": ""foo"" }";
P2 chýba z json. Chcem vedieť, kedy to tak je.
Vďaka.
odpovede:
10 pre odpoveď č. 1Musíte nastaviť vlastnosť P2 na povinné JsonPropertyAttribute
public class ApiMessage
{
public string P1 { get; set; }
[JsonProperty(Required = Required.Always)]
public string P2 { get; set; }
}
Pomocou vášho príkladu dostanete jeden JsonSerializationException
.
Dúfam, že to pomôže!