如何使用SimpleJSON将指定JSON反序列化为PlayerHandler列表?
SimpleJSON反序列化SaveValues到List解决方案
Alright, let's get your SaveValues deserialized properly with SimpleJSON. The main issue with your current code is that you're using the same index i for both the top-level array elements and their internal SaveValues arrays—those are two separate collections, so you need a nested loop to handle them. Also, since SimpleJSON doesn't automatically map JSON nodes to your custom PlayerHandler class, you'll need to manually parse each field and convert them to the right Unity types.
Here's the fixed code for your loading method:
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"]; // 测试时能获取该反序列化值 List<PlayerHandler> SaveValuesDeserialize = new List<PlayerHandler>(); // 遍历顶级数组中的每个元素(每个元素包含一组SaveValues和NoteValues) for (int i = 0; i < JNode.Count; i++) { // 获取当前顶级元素下的SaveValues数组 JSONNode saveValuesArray = JNode[i]["SaveValues"]; // 遍历SaveValues数组中的每个玩家对象 for (int j = 0; j < saveValuesArray.Count; j++) { JSONNode playerNode = saveValuesArray[j]; // 手动解析每个字段并转换为对应类型 int id = playerNode["id"].AsInt; // 解析Vector2类型的allposition Vector2 allposition = new Vector2( playerNode["allposition"]["x"].AsFloat, playerNode["allposition"]["y"].AsFloat ); // 解析Quaternion类型的allrotation Quaternion allrotation = new Quaternion( playerNode["allrotation"]["x"].AsFloat, playerNode["allrotation"]["y"].AsFloat, playerNode["allrotation"]["z"].AsFloat, playerNode["allrotation"]["w"].AsFloat ); // 解析Vector2类型的allscale Vector2 allscale = new Vector2( playerNode["allscale"]["x"].AsFloat, playerNode["allscale"]["y"].AsFloat ); // 解析Vector3类型的linepos0 Vector3 linepos0 = new Vector3( playerNode["linepos0"]["x"].AsFloat, playerNode["linepos0"]["y"].AsFloat, playerNode["linepos0"]["z"].AsFloat ); // 解析Vector3类型的linepos1 Vector3 linepos1 = new Vector3( playerNode["linepos1"]["x"].AsFloat, playerNode["linepos1"]["y"].AsFloat, playerNode["linepos1"]["z"].AsFloat ); int movetype = playerNode["movetype"].AsInt; // 创建PlayerHandler实例并添加到列表 PlayerHandler player = new PlayerHandler(id, allposition, allrotation, allscale, linepos0, linepos1, movetype); SaveValuesDeserialize.Add(player); } } // 现在SaveValuesDeserialize就包含了所有SaveValues中的玩家对象 }
Key Points Explained:
- Nested Loops: The outer loop iterates over each top-level element in your JSON array, while the inner loop processes every player object inside that element's
SaveValuesarray. This ensures you don't miss any entries and avoid index mismatches. - Manual Type Conversion: SimpleJSON returns generic
JSONNodeobjects, so you need to use methods like.AsIntand.AsFloatto convert values to basic types, then construct Unity's structs (Vector2, Quaternion, Vector3) manually. - Constructor Usage: We leverage your existing
PlayerHandlerconstructor to neatly package the parsed values into a class instance, which we then add to the list.
内容的提问来源于stack exchange,提问作者zyonneo
相关产品推荐
相关产品推荐

