0

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:

print (jsonString) output

However, the code throws NullReference exception from print (dummyPlayer[0].playerLoc).

NullReference

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?

  • JsonHelper.FromJson will returns instance of array, not an array of Player. I believe, you need to deserialize it corrctly – Akash KC Oct 07 '17 at 22:36
  • 1
    the NullReferenceException doesn't even point to anyhting that you show here... – Vogel612 Oct 07 '17 at 22:40

2 Answers2

1

The problem is that you're trying to deserialize an Object into an Array, which won't work. What fits into Player[] would be this:

[
    {
        "playerId" : "8484239823",
        "playerLoc" : "Powai",
        "playerNick" : "Random Nick"
    },
    {
        "playerId" : "512343283",
        "playerLoc" : "User2",
        "playerNick" : "Rand Nick 2"
    }
]

Observe the diference between this and the JSON object you currently attempt to deserialize and the answer should be clear

Vogel612
  • 5,620
  • 5
  • 48
  • 73
  • I tried that. Did not work. Unity does not inherently support array JSON serialization so you must user helper functions. Please read the answer to other stackoverflow question that I posted. The jsonHelper function requires that that the array be wrapped around in a JSON object. – Sebastian Roubert Oct 07 '17 at 22:46
  • @SebastianRoubert : Not sure about JsonHelper. You may need to use `dummyPlayer = JsonHelper.FromJson` as dummyPlayer is array of player. – Akash KC Oct 07 '17 at 22:59
  • I fixed the problem. The problem was that I defined fixJson as: string fixJson(string value) { value = "{\n" + "\"Player\""+ ": " + value + "\n" + "}"; return value; }.... BUT you must make it Items because the way jsonHelper is defined. – Sebastian Roubert Oct 07 '17 at 23:00
0

Fixed it.

The problem was in my fixJson function. I defined it like the following:

string fixJson(string value)
{
    value = "{\n" + "\"Player\""+ ": " + value + "\n" + "}";
    return value;
}

But you must define it exactly like this:

string fixJson(string value)
{
    value = "{\n" + "\"Items\""+ ": " + value + "\n" + "}";
    return value;
}

This is because "Items" is used explicitly in the jsonHelper function.