JsonSerializationException:找不到用于类型“custom class”的构造函数

wvt8vs2t  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(52)

我知道这个问题已经出现了很多,但我正在寻找答案,并不能真正找到一个适合我,因此我请求你的帮助:
我有一个自定义类[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:找不到用于类型Loadout的构造函数。类应该具有默认构造函数、一个带参数的构造函数或一个用JsonConstructor属性标记的构造函数。路径“[0]. name”,第1行,位置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编程。
感谢你的帮助.

8nuwlpux

8nuwlpux1#

异常消息是自我解释的。你没有Loadout类的默认构造函数。所以在你的Loadout类中,你需要添加

Loadout() {}

字符串

clj7thdc

clj7thdc2#

我得到了同样的错误。对我来说,解决方案是改变 * 管理剥离水平 * 在播放器设置从 * 低 * 到 * 最小 *。


的数据

相关问题