我有一個 TsEnum 類,我在其中存盤所有列舉值。我只是在串列中添加了一個額外的值,但它不會反序列化字串。所有其他列舉值都可以反序列化,而不是我剛剛添加的最新值。
代碼
var x = (SentencePartModelBase)JsonConvert.DeserializeObject(value.ToString(), type);
字串反序列化
{{
"Type": 204,
"Units": {
"Id": "41",
"Name": "mg",
"GroupName": "1"
},
"SequenceOrder": 2,
"IsInvalid": true
}}
[TsEnum]
public enum MaxDoseUnits
{
[EnumDisplayName("international units")]
[EnumGroupName("2")]
[EnumDictionaryId("f4ac115b-5bb1-4653-83af-3a4bef9a80e1")]
[EnumOrder("")]
Iu = 41,
}
uj5u.com熱心網友回復:
只需將 [EnumMember(Value = "Id")] 屬性添加到列舉
var x = JsonConvert.DeserializeObject<Root>(json);
班級
public enum MaxDoseUnits
{
[EnumMember(Value = "Iu")]
[EnumDisplayName("international units")]
[EnumGroupName("2")]
[EnumDictionaryId("f4ac115b-5bb1-4653-83af-3a4bef9a80e1")]
[EnumOrder("")]
Iu = 41
}
public class Units
{
public MaxDoseUnits Id { get; set; }
public string Name { get; set; }
public string GroupName { get; set; }
}
public class Root
{
public int Type { get; set; }
public Units Units { get; set; }
public int SequenceOrder { get; set; }
public bool IsInvalid { get; set; }
}
進行測驗,您也可以毫無問題地反序列化此 json
{
"Type": 204,
"Units": {
"Id": "Iu", //or "Id":41, works too
"Name": "mg",
"GroupName": "1"
},
"SequenceOrder": 2,
"IsInvalid": true
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407655.html
標籤:
下一篇:我無法從ajax中提取資料
