Я пытаюсь загрузить сохраненный класс массива с именем "Grid", который является массивом класса с именем "location", используя Newtonsoft.Json.JsonConvert.DeserializeObject, и получаю ошибку. Вот ошибка
JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'town_build_1_controller_individual_blocks+Grid' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array
Но даже несмотря на то, что я искал документацию newtonsoft, я не знаю, как это исправить
это код, вызывающий ошибку
using (StreamReader fileR = new StreamReader(("savedata/buildingtown1/" + filename)))
{
string gridinfo2 = fileR.ReadLine();
Grid endgridinfo = new Grid();
Newtonsoft.Json.JsonConvert.DeserializeObject<Grid>(gridinfo2);
print(gridinfo2);
visualhandler(endgridinfo);
print("read info" + endgridinfo.grid[0, 0].Type);
fileR.Close();
}
Строка
Newtonsoft.Json.JsonConvert.DeserializeObject<Grid>(gridinfo2);
Это мое определение для Grid:
public class Grid
{
//public Location [,] grid = new Location [3, 3];
public Location [,] grid = { { new Location(), new Location(), new Location() }, { new Location(), new Location(), new Location() }, { new Location(), new Location(), new Location() } };
}
Это мое определение для местоположения:
public class Location
{
public int Type { get; set; }
public int Level { get; set; }
}
файл, который я пытаюсь загрузить, содержит это
[{"Type":1,"Level":0},{"Type":0,"Level":0},{"Type":0,"Level":0}],
[{"Type":0,"Level":0},{"Type":0,"Level":0},{"Type":0,"Level":0}],
[{"Type":0,"Level":0},{"Type":0,"Level":0},{"Type":0,"Level":0}]
спасибо за вашу помощь, и я знаю, что я довольно плохо разбираюсь в кодировании, поэтому, пожалуйста, не просто комментируйте это Большое спасибо, law_man123