- Home /
json property array into unity
Hi, I have json as below:
{
"level":"0",
"layers":[3,2],
}
and I just want to have it imported into Unity. I have serialized object as below:
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class StageData
{
public int level; // current level
public float[] layers;
}
and I imported as below:
StageData stage = JsonUtility.FromJson<StageData> (json);
"level" property imported successfully but array property "layers" is coming in as empty array - [].. Any suggestions?
I've never used JSON import, but i guess the variable names must match and "layer" != "layers".
corrected the typo in the questions, thanks!
Sorry i didn't see your edit and added an answer. Is it still not working?
What happens if you remove the comma after [3,2] ?
I tested with the following code and it worked:
[System.Serializable]
public class StageData
{
public int level; // current level
public float[] layers;
}
string json = "{ \"level\": \"0\", \"layers\": [3,2] }";
StageData stage = JsonUtility.FromJson<StageData> (json);
Debug.Log( stage.level );
Debug.Log( stage.layers[0] );
Debug.Log( stage.layers[1] );
Answer by marchall_box · Nov 09, 2016 at 12:29 PM
It was post's typo, actual code has same variable name - its still same result..
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I Open/Close my option menu with one key C# 1 Answer
Should i create animations for different directions for a single animation? 0 Answers
Help using LitJson 1 Answer
Distribute terrain in zones 3 Answers