Here is my JSON file for testing purposes :
{"timestamp":4455518515,"profileId":"5asd15qwe11as2d12q1sd","profileName":"player01",
"textures":{"material":{"url":"http://materialurl.com/aksjd54854we5a4sd.png"}}}
I want to reach textures -> material -> url but i can't. Here is my code :
public class JsonTest : MonoBehaviour {
string json;
string path;
// Use this for initialization
void Start () {
path = Application.streamingAssetsPath + "/json.txt";
json = File.ReadAllText(path);
MaterialJson test = JsonUtility.FromJson<MaterialJson>(json);
Debug.Log(test.timestamp);
Debug.Log(test.profileId);
Debug.Log(test.profileName);
Debug.Log(test.textures);
}
}
[Serializable]
public class MaterialJson
{
public double timestamp;
public string profileId;
public string profileName;
public string textures;
}
There is no problem for first three variables. But textures
returns null in debug log. I have no idea how can I take nested variables in textures
. Is unity's JsonUtility
capable of that ? I couldn't find any info on unity's documentation.
It's worked perfectly with the code below : (Thanks to @mrfreester)
[Serializable]
public class MaterialJson
{
public double timestamp;
public string profileId;
public string profileName;
public Textures textures;
}
[Serializable]
public class Textures
{
public Material material;
}
[Serializable]
public class Material
{
public string url;
}
Debug.Log(test.textures.material.url);