我知道这个问题已经出现了很多次,但我正在查看答案,但找不到真正适合我的答案,因此我向您寻求帮助:
我有一个自定义类 [Loadout]:
public class Loadout // used to save a players loadout on the server
{
public string name;
public float loadoutID;
public SO_Admiral admiral;
public FM_ShipSave[] shipsInTheLoadout;
public bool selectedLoadout;
public Loadout(string _name, FM_ShipSave[] _ShipSavesArray)
{
name = _name; // later this must come from an input-field
loadoutID = Random.Range(0f, 100000000f);
shipsInTheLoadout = _ShipSavesArray;
}
public Loadout(string _name)
{
name = _name;
shipsInTheLoadout = new FM_ShipSave[0];
}
}
它还包括其他自定义类,例如:
public class FM_ShipSave
{
// this class saves ID and position in the array of the ship
public int shipID;
public int tileX;
public int tileZ;
public FM_ShipSave(UnitScript _unit)
{
shipID = _unit.SO_ofThis.getPositioninAllUnitsArray();
tileX = _unit.getTileX;
tileZ = _unit.getTileZ;
}
}
我将其序列化为 .Json 文件,将其发送到服务器并将其存储在那里。序列化部分完美运行:
var request = new UpdateUserDataRequest // after what is supposed to happen with the current loadout is determined we upload it to the server
{
Data = new Dictionary<string, string> // create a new dicitonary
{
{"Loadouts", JsonConvert.SerializeObject(playerLoadouts)} // convert the List<Loadout> playerLoadouts into a .json file
}
};
PlayFabClientAPI.UpdateUserData(request, onDataSend, OnError); // then send it to the server, to be stored
Link to picture of Json-editor
但是一旦我尝试反序列化它:
List<Loadout> playerLoadouts = JsonConvert.DeserializeObject<List<Loadout>>(_result.Data["Loadouts"].Value); // turn the jsonback into a List<Loadout>
我收到以下错误:
Blockquote JsonSerializationException: Unable to find a constructor to use for type Loadout. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path '[0].name', line 1, position 9.
我也尝试过使用不同的更简单的类,一切正常:
public class JsonTest
{
public string name;
public JsonTest(string _name)
{
name = _name;
}
}
发送:
// to test serialization
JsonTest _test = new JsonTest("test");
var request2 = new UpdateUserDataRequest // after what is supposed to happen with the current loadout is determined we upload it to the server
{
Data = new Dictionary<string, string> // create a new dicitonary
{
{"test", JsonConvert.SerializeObject(_test)} // convert the List<Loadout> playerLoadouts into a .json file
}
};
检索:
public void testDeserialization(GetUserDataResult _result)
{
JsonTest _retrievedTest = JsonConvert.DeserializeObject<JsonTest>(_result.Data["test"].Value); // turn the jsonback into a List<Loadout>
Debug.Log("retrieved test deserialization name: " + _retrievedTest.name);
}
因此我得出结论,这与我的自定义类有关,但我不知道如何修复它。我应该编写一个自定义解串器吗?或者是否有不同的解决方案?
我按照本教程创建和上传 json 文件: Link to tutorial on youtube
我正在使用 Visual Studio 在 Mac 上进行 Unity 编码。
感谢您的帮助。
最佳答案
异常消息是不言自明的。 Loadout 类没有默认构造函数。因此,在您的 Loadout 类中,您需要添加
Loadout() {}
关于c# - 再次: JsonSerializationException: Unable to find a constructor to use for type "custom class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71982374/