再次:JSonSerializationException:无法找到用于类型的构造函数“自定义类”
Quesiton已经出现了很多
我
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
链接到JSON-EDITOR的图片
List<Loadout> playerLoadouts = JsonConvert.DeserializeObject<List<Loadout>>(_result.Data["Loadouts"].Value); // turn the jsonback into a List<Loadout>
我收到以下错误:
blockquote jsonserializationException:无法找到用于类型加载的构造函数。一个类应具有默认构造函数,一个带有参数的构造函数或标记为jsonconstructor属性的构造函数。路径'[0] .name',第1行,位置9。
我也尝试了一个更简单的类,一切正常:
public class JsonTest
{
public string name;
public JsonTest(string _name)
{
name = _name;
}
}
send:
// 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-files:
”在Mac上使用Visual Studio。
感谢您的帮助。
I know this quesiton has already come up a lot, but I was looking through the answers and could not really find one that fits me, therefore I am asking you for help:
I have a custom class [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];
}
}
it also includes other custom classes like:
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;
}
}
that I serialize into a .Json-file to send it to a server and store it there. The serialization part works perfectly:
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
but as soon as I try to deserialize it:
List<Loadout> playerLoadouts = JsonConvert.DeserializeObject<List<Loadout>>(_result.Data["Loadouts"].Value); // turn the jsonback into a List<Loadout>
I am getting the following error:
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.
I have also tried it with a different simpler class and there everything works fine:
public class JsonTest
{
public string name;
public JsonTest(string _name)
{
name = _name;
}
}
Send:
// 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
}
};
Retrieve:
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);
}
I therefore conclude that it has something to do with my custom classes, but I don't know how to fix it. Should I have to write a custom deserializer? Or is there maybe a different solution?
I was following this tutorial to create and upload json-files:
Link to tutorial on youtube
I am coding for unity on a mac using visual studio.
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
例外消息是自我解释。您没有类负载的默认构造函数。因此,在您的加载课中,您需要添加
The exception message is self explaining. You don't have a default constructor for the class Loadout. So in your Loadout class you need to add
我遇到了同样的错误。对我来说,解决方案是将在播放器设置中更改 low minimal 。
I was getting the same error. The solution for me was changing Managed Stripping Level in Player Settings from Low to Minimal.