-1

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

FredM
  • 454
  • 9
  • 20
  • Deserialize into a `List>` and build your grid from there. – CodeCaster Nov 21 '18 at 10:30
  • How would I do that, could You give me an example snippet. Sorry this is my first time doing something as complicated as this – law_man123 Nov 21 '18 at 10:31
  • I just tried it but List is not recognised, what should I do – law_man123 Nov 21 '18 at 10:40
  • Import the proper namespace, namely `System.Collections.Generic`. Then call `JsonConvert.DeserializeObject>()`. – CodeCaster Nov 21 '18 at 10:56
  • but how would I turn it back into a grid – law_man123 Nov 21 '18 at 11:03
  • Does your file contain an extra `[` at the beginning and an extra `]` at the end? If not, it looks to be [newline-delimited JSON](http://ndjson.org/) rather than a single JSON container. To read newline-delimited JSON see [Line delimited json serializing and de-serializing](https://stackoverflow.com/q/29729063). – dbc Nov 21 '18 at 17:23

1 Answers1

0

You're not deserializing a grid object your deserialzing a Location[,].

var result =   Newtonsoft.Json.JsonConvert.DeserializeObject<Location[,]>(gridinfo2);
Grid endgridinfo = new Grid(){ grid = result };

This is what it would have to look like for your code to work as is. When trying to deserialize something that complicated I would try to serialize what you're expecting it to go into and check for difference.

{
    "grid": [
      [
        {
          "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
        },
        {
          "Type": 0,
          "Level": 0
        }
      ]
    ]
  }
Ryan Schlueter
  • 2,223
  • 2
  • 13
  • 19