我想反序列化包含至少2个不同列表的json,这些列表包含继承的对象。
这是我的json:
{
"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);
}
}
LevelGeneration的代码不起作用。我想用json填充我的Mobile,Immobile,Life + 1和JumpBoost课程
我已经看过有关继承列表的主题: Json.net serialize/deserialize derived types? http://gigi.nullneuron.net/gigilabs/deserializing-derived-types-with-json-net/
但从不使用多个列表
请帮助我了解我该怎么做!谢谢!