So I'm trying to serialize a Json array from my computer.
I followed the answer to this question: Serialize and Deserialize Json and Json Array in Unity
The only difference being that the source of my JSON is from my desktop.
I have the JsonHelper function, fixJson function, and the player class in my file.
In my Start function I have the following code:
jsonString = fixJson(System.IO.File.ReadAllText("/Users/sroubert/Desktop/playerDummyDataTest.json"));
dummyPlayer = JsonHelper.FromJson<Player> (jsonString);
print (jsonString);
print (dummyPlayer[0].playerLoc);
I define dummyPlayer at the top of my file with the following:
public Player[] dummyPlayer;
The print (jsonString)
command is working correctly. It outputs the following:
However, the code throws NullReference exception from print (dummyPlayer[0].playerLoc).
I tested the jsonHelper function with the following:
string jsonString = "{\r\n \"Items\": [\r\n {\r\n \"playerId\": \"8484239823\",\r\n \"playerLoc\": \"Powai\",\r\n \"playerNick\": \"Random Nick\"\r\n },\r\n {\r\n \"playerId\": \"512343283\",\r\n \"playerLoc\": \"User2\",\r\n \"playerNick\": \"Rand Nick 2\"\r\n }\r\n ]\r\n}";
dummyPlayer = JsonHelper.FromJson<Player>(jsonString);
print (jsonString);
Debug.Log(dummyPlayer[0].playerLoc);
The print (jsonString) output is exactly the same. However, no NullReferenceException. Prints out Powai.
Does anyone know what may be going on?