У меня есть собственный класс [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;
}
}
Код: Выделить всё
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 // create a new dicitonary
{
{"Loadouts", JsonConvert.SerializeObject(playerLoadouts)} // convert the List playerLoadouts into a .json file
}
};
PlayFabClientAPI.UpdateUserData(request, onDataSend, OnError); // then send it to the server, to be stored
но как только я попытаюсь ее десериализовать:
Код: Выделить всё
List playerLoadouts = JsonConvert.DeserializeObject(_result.Data["Loadouts"].Value); // turn the jsonback into a List
Blockquote
JsonSerializationException: невозможно найти конструктор для использования для типа загрузки. Класс должен иметь конструктор по умолчанию, один конструктор с аргументами или конструктор, отмеченный атрибутом 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 // create a new dicitonary
{
{"test", JsonConvert.SerializeObject(_test)} // convert the List playerLoadouts into a .json file
}
};
Код: Выделить всё
public void testDeserialization(GetUserDataResult _result)
{
JsonTest _retrievedTest = JsonConvert.DeserializeObject(_result.Data["test"].Value); // turn the jsonback into a List
Debug.Log("retrieved test deserialization name: " + _retrievedTest.name);
}
Я следовал этому руководству по созданию и загрузке json-файлов:
Ссылка на руководство на YouTube
Я пишу код для Unity на Mac с помощью Visual Studio.
Спасибо за помощь.
Подробнее здесь: https://stackoverflow.com/questions/719 ... to-use-for
Мобильная версия