【发布时间】:2019-10-19 09:41:42
【问题描述】:
我有以下 JSON。
[{"SaveValues":[{"id":1,"allposition":{"x":-4.888263702392578,"y":4.799035549163818},"allrotation":{"x":0.0,"y" :0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0 ,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":1},{"id":2,"allposition": {"x":-4.607616424560547,"y":-4.033360004425049},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale": {"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0," y":0.0,"z":0.0},"movetype":1}],"NoteValues":[{"movenumber":1,"notemsg":"ssd"}]},{"SaveValues":[{ "id":1,"allposition":{"x":-4.888263702392578,"y":4.799035549163818},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w ":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1" :{"x":0.0,"y":0.0,"z":0.0},"movetype":2},{"id":2,"allposition":{"x":-4.607616424560547,"y" :-4.033360004425049},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y": 1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0," z":0.0},"movetype":2},{"id":2,"allposition":{"x":4.328777313232422,"y":1.9337825775146485},"allrotation":{"x":0.0," y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y" :0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2}],"NoteValues":[{"movenumber ":2,"notemsg":"ddf"}]}]
它们用于序列化的类如下所示。
[System.Serializable]
public class PlayerHandler
{
public int id;
public Vector2 allposition;
public Quaternion allrotation;
public Vector2 allscale;
public Vector3 linepos0;
public Vector3 linepos1;
public int movetype;
public PlayerHandler(int ids,Vector2 allpos,Quaternion allrot,Vector2 allscal,Vector3 Line0,Vector3 Line1,int Moves)
{
this.id = ids;
this.allposition = allpos;
this.allrotation = allrot;
this.allscale = allscal;
this.linepos0 = Line0;
this.linepos1 = Line1;
this.movetype = Moves;
}
}
[System.Serializable]
public class PlayerMovement
{
public int movenumber;
public string notemsg;
public PlayerMovement(int Movenum,string Note)
{
this.movenumber = Movenum;
this.notemsg = Note;
}
}
在加载方法中我试图反序列化 JSON。所以我创建了一个列表
List<PlayerHandler> SaveValuesDeserialize = new List<PlayerHandler>();
我希望从 Json 中添加 SaveValues。我尝试过的方法如下。
using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open))
{
fs.Dispose();
jsonLoadstring = File.ReadAllText(Path.Combine(Application.persistentDataPath, filename));
JSONNode JNode = SimpleJSON.JSON.Parse(jsonLoadstring);
var deseiralized = JNode[0]["NoteValues"][0]["notemsg"];//Here I am getting the deserialized value for testing.
//What should I do to get the SaveValues inside the list
for (int i = 0; i < JNode.Count; i++)
{
SaveValuesDeserialize.Add(JNode[i]["SaveValues"][i]);
}
}
我希望使用简单的 JSON 来解决这个问题。我尝试使用其他方法来反序列化,这有点难以理解。所以选择这种方法来尝试。因为我得到了测试值,我希望我能得到使用 SIMPLEJSON 的值。
【问题讨论】: