Why are my arrays in arrays null (converting from JSON)?
Very new to c# from the www JavaScript world, I'm having troubles with partial success in getting loaded JSON converted into a usable object.
After loading the JSON, I can create an object with the loaded name present (success!). The positions are NULL however. It's likely some scope issue but I'm not seeing it yet.
For my game puzzle layout, the JSON follows this structure:
{
"name": "Traditional 1",
"positions": [
[
{"x":6, "y":2},{"x":8, "y":2}
],
[
{"x":12, "y":4},{"x":14, "y":4},{"x":16, "y":4}
],
[
{"x":14, "y":6},{"x":16, "y":6},{"x":18, "y":6},{"x":20, "y":6}
]
]
}
Following examples from the docs, I created a Layout class (issue here likely, multidimensional arrays not in example docs of course):
public class Layout
{
public string name;
public LayoutPos[][] positions; //!!! issue?!! (positions in array, in array)
}
I also created a LayoutPos class to store slots where game pieces will be placed (not a Vector2 per say):
public class LayoutPos
{
public int x;
public int y;
}
Then I load the json into a Layout object:
void loadLayoutData (int layoutInt) {
string filePath = "Layouts/" + layoutInt.ToString("D5");
TextAsset layoutFile = Resources.Load<TextAsset>(filePath);
Layout layoutData = JsonUtility.FromJson<Layout>(layoutFile.text);
Debug.Log (layoutData.name); //success
Debug.Log (layoutData.positions); //!!! NULL
}
Any thoughts on how I would correct my positions reference? Thank you for taking a look.
Answer by hexagonius · Oct 25, 2016 at 08:09 PM
I just searched the manual page and found the following under Supported Types :
Passing other types directly to the API, for example primitive types or arrays, is not currently supported. For now you will need to wrap such types in a class or struct of some sort.
I guess this means you'll have to create something else you can then create an array from.
Thank you for that answer. Hmmm. $$anonymous$$aybe I could create some parse-able string and recreate the arrays that way after being loaded. I'm also seeing mention of other JSON parsers.
Your answer
Follow this Question
Related Questions
Index out of range error 1 Answer
Null reference exception in an if statement that checks for it. 1 Answer
Convert string to array name? 0 Answers
Can't access public static array from another class 1 Answer
public vs private array in classes 0 Answers