我希望在Unity中反序列化它,并且已经取得了一些成功,但是似乎停留在双数组上。这是一些JSON(仅显示相关内容)
{ "cities": [{ "name": "London", "monuments": [{ "levels": 15, "objBeingIntroduced": "none" }, { "levels": 25, "objBeingIntroduced": "df" } ] }], "puzzles": [{ "puzzleId": 1, "moves": [ [{ "x": 3, "y": 3, "xInc": 1, "yInc": 0 }, { "x": 5, "y": 3, "xInc": -1, "yInc": 0 } ], [{ "x": 4, "y": 3, "xInc": 0, "yInc": 1 }, { "x": 4, "y": 5, "xInc": 0, "yInc": -1 } ] ], "squares": [{ "x": 3, "y": 3, "type": "d" }, { "x": 5, "y": 3, "type": "d" }, { "x": 4, "y": 5, "type": "d" } ] }]
}
您将如何在Unity中的JSON中反序列化此代码?
我可以抓住拼图部分的所有细节,除了移动类别之外,因为这是一个双数组。这是我到目前为止所拥有的
[System.Serializable]
public class LevelStructure
{
public int puzzleId;
public List<Moves> moves = new List<Moves>();
public Squares[] squares;
}
[System.Serializable]
public class Levels
{
public LevelStructure[] result;
}
[System.Serializable]
public class Squares
{
public int x;
public int y;
public string type;
}
[System.Serializable]
public class Moves
{
public Move[] moves;
}
[System.Serializable]
public class Move
{
public int x;
public int y;
public int xInc;
public int yInc;
}
我似乎无法弄清楚如何处理“纪念碑”和“动作”部分之类的双精度数组。对于从这里出发的任何建议,我们将不胜感激。
由于我认为这种结构很好,因此我实际上如何将json加载到这些类中?
谢谢
答案 0 :(得分:1)
虽然您应该真正考虑一下自己的结构并编写自己的代码,但这是json2csharp.com可以为您做的事情:
public class Monument
{
public int levels { get; set; }
public string objBeingIntroduced { get; set; }
}
public class City
{
public string name { get; set; }
public List<Monument> monuments { get; set; }
}
public class Square
{
public int x { get; set; }
public int y { get; set; }
public string type { get; set; }
}
public class Puzzle
{
public int puzzleId { get; set; }
public List<List<>> moves { get; set; }
public List<Square> squares { get; set; }
}
public class RootObject
{
public List<City> cities { get; set; }
public List<Puzzle> puzzles { get; set; }
}