[英]Newtonsoft Json Deserialization 1 json property to decimal or int c# properties
[英]Newtonsoft json deserialization - key as property
我有一個json文件:
...
"Key1": {
"Base": 123,
"Max": 1234
},
"Key2": {
"Base": 123,
"Max": 1234
},
"Key3": {
"Base": 123,
"Max": 1234
},
...
我需要使用JsonConvert.DeserializeObject<T>(__json);
將其反序列化為一個對象JsonConvert.DeserializeObject<T>(__json);
但是我需要將鍵(Key1,Key2,Key3等)用作反序列化對象的屬性。 不幸的是,我無法使用其他反序列化方法,也無法修改json格式。
我的對象就是這樣
public class Item {
public string Id { get; set; }
public int Base { get; set; }
public int Max { get; set; }
}
我的ID應該是“ Key1”,“ Key2 ...”
可能嗎?
制作一個自定義的Key
類:
public class Key {
public int Base { get; set; }
public int Max { get; set; }
}
然后將JSON結果中的每個項目存儲在Dictionary
,其中的鍵是鍵名,其值是Key
項:
var keyCollection = new Dictionary<string, Key>();
//you can then do stuff such as:
var maxOfKeyOne = keyCollection["Key1"].Max;
var baseOfKeyTwo = keyCollection["Key2"].Base;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.