I am trying to load a saved array class called "Grid" which is an array of a class called "location" using Newtonsoft.Json.JsonConvert.DeserializeObject and am getting an error Here is the error
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
But even though I have scoured the newtonsoft documentation I don't know how to fix it
this is the code that is causing the error
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();
}
The line is
Newtonsoft.Json.JsonConvert.DeserializeObject<Grid>(gridinfo2);
This is my definition for 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() } };
}
this is my definition for Location:
public class Location
{
public int Type { get; set; }
public int Level { get; set; }
}
the file that I am trying to load contains this
[{"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}]
thanks for your help, and I know im quite bad at coding so please don't just comment that Many thanks, law_man123
>` and build your grid from there.
– CodeCaster Nov 21 '18 at 10:30>()`.
– CodeCaster Nov 21 '18 at 10:56