再次:JsonSerializationException:无法找到用于类型 "custom class" 的构造函数

Once again: JsonSerializationException: Unable to find a constructor to use for type "custom class"

我知道这个问题已经有很多人提出了,但是我翻了翻答案,找不到真正适合我的答案,所以我向你求助:

我有一个自定义 class [加载]:

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];
    }   
}

它还包括其他自定义 class,例如:

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.

我也用不同的更简单的 class 尝试过,一切正常:

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);
    }

因此我得出结论,它与我的自定义 classes 有关,但我不知道如何解决它。我应该写一个自定义反序列化器吗?或者是否有不同的解决方案?

我按照本教程创建和上传 json 文件: Link to tutorial on youtube

我正在使用 visual studio 在 mac 上编写统一代码。

感谢您的帮助。

异常消息不言自明。您没有 class 加载项的默认构造函数。所以在你的 Loadout class 你需要添加

 Loadout() {}