我有一些JSON看起来像这样:
{
"account_id": 4294967295,
"player_slot": 0,
"hero_id": 93,
"item_0": 170,
"item_1": 160,
"item_2": 81,
"item_3": 208,
"item_4": 63,
"item_5": 0,
"kills": 17,
"deaths": 6,
"assists": 8,
"leaver_status": 0,
"gold": 2978,
"last_hits": 80,
"denies": 0,
"gold_per_min": 434,
"xp_per_min": 630,
"gold_spent": 19090,
"hero_damage": 16232,
"tower_damage": 229,
"hero_healing": 522,
"level": 25,
"ability_upgrades": [ <snip> ]
}
我想要序列化到我的模型,就像这样:
[JsonConverter(typeof(MatchDetailPlayerConverter))]
public class MatchDetailPlayer
{
public int AccountId { get; set; }
public PlayerSlot PlayerSlot { get; set; }
public Hero Hero { get; set; }
public List<Item> Items { get; set; }
public int Kills { get; set; }
public int Deaths { get; set; }
public int Assists { get; set; }
public LeaverStatus LeaverStatus { get; set; }
public int Gold { get; set; }
public int LastHits { get; set; }
public int Denies { get; set; }
public int GoldPerMin { get; set; }
public int XpPerMin { get; set; }
public int GoldSpent { get; set; }
public int HeroDamage { get; set; }
public int TowerDamage { get; set; }
public int HeroHealing { get; set; }
public int Level { get; set; }
public List<AbilityUpgrade> AbilityUpgrades { get; set; }
public List<AdditionalUnit> AdditionalUnits { get; set; }
}
JSON中有6个名为item_x
的属性,它们是项目的id。我想将这6个键/值反序列化为一个List<Item>
。我的item类是一个以item id作为值的enum。
我写了一个自定义的JsonConverter,可以在这里找到。我的问题是,当转换器的ReadJson()
方法中的jo.ToObject<MatchDetailPlayer>()
时,转换器一直遇到堆栈溢出。
是什么导致了这个无限循环?是否有更好的方法将许多键/值反序列化为单个对象的列表?
我认为您可以查看一下类中JsonExtensionData属性的实现,以手动修改反序列化你可以在这里找到一个例子:Json序列化属性