C# 使用继承反序列化多个列表
我想反序列化包含至少2个不同列表的json,这些列表包含继承的对象 这是我的json:C# 使用继承反序列化多个列表,c#,json,inheritance,serialization,C#,Json,Inheritance,Serialization,我想反序列化包含至少2个不同列表的json,这些列表包含继承的对象 这是我的json: { "difficulty": 1, "plateformes": [ { "$type" : "Immobile, Assembly-CSharp", "x": 19, "y": 4.5, "friable": false }, {
{
"difficulty": 1,
"plateformes": [
{
"$type" : "Immobile, Assembly-CSharp",
"x": 19,
"y": 4.5,
"friable": false
},
{
"$type" : "Mobile, Assembly-CSharp",
"x": 40,
"y": 4.5,
"finX": 45,
"finY": 4.5
}],
"items": [
{
"$type": "Jumpboost, Assembly-CSharp",
"temporaire": true,
"x": 34,
"y": 5,
"duree": 10
},
{
"$type": "Life+1, Assembly-CSharp",
"temporaire": false,
"x": 74,
"y": 5
}]
}
以下是我的课程:
public class Level{
public int difficulte;
public List<Plateforme> plateformes { get; set; }
public List<Items> items { get; set; }
...
}
public class Plateforme {
public int largeur;
public float positionX;
public float positionY;
}
public class Mobile : Plateforme {
public float positionFinX;
public float positionFinY;
}
public class Immobile : Plateforme {
public bool friable;
}
public class Items {
private bool temporaire;
private float x;
private float y;
}
public class Life+1 : Items {}
public class JumpBoost : Items{}
public class LevelGeneration {
Level level = new Level();
void Start () {
string json = JsonConvert.SerializeObject(level, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
level = JsonConvert.DeserializeObject<Level>(json, settings);
}
}
公共类级别{
公共交通困难;
公共列表平台{get;set;}
公共列表项{get;set;}
...
}
公共级平台{
公共场所;
公共浮动头寸x;
公共浮动头寸;
}
公共类手机:Platforme{
公共金融机构;
公共财政;
}
公共类不动:平台{
公共场所易碎;
}
公共类项目{
私人住宅;
私人浮动x;
私人游船;
}
公共类生命+1:项目{}
公共类JumpBoost:项{}
公共类级别生成{
级别=新级别();
无效开始(){
string json=JsonConvert.SerializeObject(级别、格式、缩进、新JsonSerializerSettings
{
TypeNameHandling=TypeNameHandling.Auto
});
level=JsonConvert.DeserializeObject(json,设置);
}
}
LevelGeneration的代码不起作用。我想用json填充我的移动类、固定类、Life+1类和JumpBoost类
我已经看过关于继承列表的主题:
但千万不要列出几个清单
请帮助我了解我该怎么做!谢谢